Exemplo n.º 1
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                context.ExecuteStoreCommand("insert into chapter2.account (DeletedOn,AccountHolderId) values ('2/10/2009',1728)");

                var account = new Account {
                    AccountHolderId = 2320
                };
                context.Accounts.AddObject(account);
                account = new Account {
                    AccountHolderId = 2502
                };
                context.Accounts.AddObject(account);
                account = new Account {
                    AccountHolderId = 2603
                };
                context.Accounts.AddObject(account);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                foreach (var account in context.Accounts)
                {
                    Console.WriteLine("Account Id = {0}", account.AccountHolderId.ToString());
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
Exemplo n.º 2
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var r1 = new Resume {
                    Title = "C# Developer", Name = "Sally Jones"
                };
                r1.ResumeDetail = new ResumeDetail {
                    Body = "...very long resume goes here..."
                };
                context.Resumes.AddObject(r1);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                var resume = context.Resumes.Single();
                Console.WriteLine("Title: {0}, Name: {1}", resume.Title, resume.Name);

                // note, the ResumeDetail is not loaded until we reference it
                Console.WriteLine("Body: {0}", resume.ResumeDetail.Body);
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
Exemplo n.º 3
0
 static void Cleanup()
 {
     using (var context = new EFRecipesEntities())
     {
         context.ExecuteStoreCommand("delete from chapter2.account");
     }
 }
Exemplo n.º 4
0
 static void Cleanup()
 {
     using (var context = new EFRecipesEntities())
     {
         context.ExecuteStoreCommand("delete from chapter11.movierental");
     }
 }
Exemplo n.º 5
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var mr1 = new MovieRental {
                    Title = "A Day in the Life", RentalDate = DateTime.Parse("2/19/2010"), ReturnedDate = DateTime.Parse("3/4/2010"), LateFees = 3M
                };
                var mr2 = new MovieRental {
                    Title = "The Shortest Yard", RentalDate = DateTime.Parse("3/15/2010"), ReturnedDate = DateTime.Parse("3/20/2010"), LateFees = 0M
                };
                var mr3 = new MovieRental {
                    Title = "Jim's Story", RentalDate = DateTime.Parse("3/2/2010"), ReturnedDate = DateTime.Parse("3/19/2010"), LateFees = 3M
                };
                context.MovieRentals.AddObject(mr1);
                context.MovieRentals.AddObject(mr2);
                context.MovieRentals.AddObject(mr3);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine("Movie rentals late returns");
                Console.WriteLine("==========================");
                var late = from r in context.MovieRentals
                           where EntityFunctions.DiffDays(r.RentalDate, r.ReturnedDate) > 10
                           select r;
                foreach (var rental in late)
                {
                    Console.WriteLine("{0} was {1} days late, fee: {2}", rental.Title, (rental.ReturnedDate - rental.RentalDate).Days - 10, rental.LateFees.ToString("C"));
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
Exemplo n.º 6
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var auth1 = new Author {
                    Name = "Jane Austin"
                };
                var book1 = new Book {
                    Title = "Pride and Prejudice", ISBN = "1848373104"
                };
                var book2 = new Book {
                    Title = "Sense and Sensibility", ISBN = "1440469563"
                };
                auth1.Books.Add(book1);
                auth1.Books.Add(book2);
                var auth2 = new Author {
                    Name = "Audrey Niffenegger"
                };
                var book3 = new Book {
                    Title = "The Time Traveler's Wife", ISBN = "015602943X"
                };
                auth2.Books.Add(book3);
                context.Authors.AddObject(auth1);
                context.Authors.AddObject(auth2);
                context.SaveChanges();
                context.DeleteObject(book1);
                context.SaveChanges();
            }

            Console.WriteLine("No output, check out the SQL Profiler to see what happens");
            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
Exemplo n.º 7
0
 static void Cleanup()
 {
     using (var context = new EFRecipesEntities())
     {
         context.ExecuteStoreCommand("delete from chapter12.[order]");
         context.ExecuteStoreCommand("delete from chapter12.orderstatus");
     }
 }
Exemplo n.º 8
0
 private Job CreateJob(string title, decimal salary)
 {
     using (var context = new EFRecipesEntities())
     {
         return(new Job {
             Title = title, Salary = salary
         });
     }
 }
Exemplo n.º 9
0
 static void Cleanup()
 {
     using (var context = new EFRecipesEntities())
     {
         context.ExecuteStoreCommand("delete from chapter10.authorbook");
         context.ExecuteStoreCommand("delete from chapter10.author");
         context.ExecuteStoreCommand("delete from chapter10.book");
     }
 }
Exemplo n.º 10
0
 static void Cleanup()
 {
     using (var context = new EFRecipesEntities())
     {
         context.ExecuteStoreCommand("delete from chapter5.appointment");
         context.ExecuteStoreCommand("delete from chapter5.doctor");
         context.ExecuteStoreCommand("delete from chapter5.patient");
     }
 }
Exemplo n.º 11
0
 static void Cleanup()
 {
     using (var context = new EFRecipesEntities())
     {
         context.ExecuteStoreCommand("delete from chapter15.[order]");
         context.ExecuteStoreCommand("delete from chapter15.customer");
         context.ExecuteStoreCommand("delete from chapter15.orderstatustype");
         context.ExecuteStoreCommand("delete from chapter15.shippingtype");
     }
 }
Exemplo n.º 12
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                context.Patients.AddObject(new Patient {
                    Name = "Jill Stevens", City = "Dallas"
                });
                context.Patients.AddObject(new Patient {
                    Name = "Bill Azle", City = "Fort Worth"
                });
                context.Patients.AddObject(new Patient {
                    Name = "Karen Stanford", City = "Raytown"
                });
                context.Patients.AddObject(new Patient {
                    Name = "David Frazier", City = "Dallas"
                });
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine("Using LINQ Builder Methods");
                var patients = context.Patients.Where(p => p.City == "Dallas");
                foreach (var patient in patients)
                {
                    Console.WriteLine("{0} is in {1}", patient.Name, patient.City);
                }
            }

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine("\nUsing Entity SQL");
                var patients = context.CreateQuery <Patient>(@"select value p from Patients as p where p.City = 'Dallas'");
                foreach (var patient in patients)
                {
                    Console.WriteLine("{0} is in {1}", patient.Name, patient.City);
                }
            }

            using (var context = new EFRecipesEntities())
            {
                Console.WriteLine("\nUsing ESQL Builder Methods");
                var patients = context.CreateObjectSet <Patient>("Patients").Where("it.City = 'Dallas'");
                foreach (var patient in patients)
                {
                    Console.WriteLine("{0} is in {1}", patient.Name, patient.City);
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
Exemplo n.º 13
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                // insert our lookup values
                context.ExecuteStoreCommand("insert into chapter15.orderstatustype(OrderStatusTypeId, Description) values (1,'Processing')");
                context.ExecuteStoreCommand("insert into chapter15.orderstatustype(OrderStatusTypeId, Description) values (2,'Shipped')");
                context.ExecuteStoreCommand("insert into chapter15.shippingtype(ShippingTypeId, Description) values (1,'UPS')");
                context.ExecuteStoreCommand("insert into chapter15.shippingtype(ShippingTypeId, Description) values (2,'FedEx')");
            }

            using (var context = new EFRecipesEntities())
            {
                var c1 = new Customer {
                    FirstName = "Robert", LastName = "Jones"
                };
                var o1 = new Order {
                    OrderDate = DateTime.Parse("11/19/2009"), OrderStatusTypeId = 2, ShippingTypeId = 1, Customer = c1
                };
                var o2 = new Order {
                    OrderDate = DateTime.Parse("12/13/09"), OrderStatusTypeId = 1, ShippingTypeId = 1, Customer = c1
                };
                var c2 = new Customer {
                    FirstName = "Julia", LastName = "Stevens"
                };
                var o3 = new Order {
                    OrderDate = DateTime.Parse("10/19/09"), OrderStatusTypeId = 2, ShippingTypeId = 2, Customer = c2
                };
                context.Customers.AddObject(c1);
                context.Customers.AddObject(c2);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                context.ContextOptions.LazyLoadingEnabled = true;
                foreach (var c in context.Customers)
                {
                    Console.WriteLine("{0} has {1} order(s)", c.FullName, c.TotalOrders.ToString());
                    foreach (var o in c.Orders)
                    {
                        Console.WriteLine("\tOrdered on: {0}", o.OrderDate.ToShortDateString());
                        Console.WriteLine("\tStatus: {0}", o.OrderStatus);
                        Console.WriteLine("\tShip via: {0}\n", o.ShippingType);
                    }
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
Exemplo n.º 14
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                // static order status
                var assemble = new OrderStatus {
                    OrderStatusId = 1, Status = "Assemble"
                };
                var test = new OrderStatus {
                    OrderStatusId = 2, Status = "Test"
                };
                var ship = new OrderStatus {
                    OrderStatusId = 3, Status = "Ship"
                };
                context.OrderStatus.AddObject(assemble);
                context.OrderStatus.AddObject(test);
                context.OrderStatus.AddObject(ship);

                var order = new Order {
                    Description = "HAL 9000 Supercomputer", OrderStatus = assemble
                };
                context.Orders.AddObject(order);
                context.SaveChanges();

                order.OrderStatus = ship;
                try
                {
                    context.SaveChanges();
                }
                catch (Exception)
                {
                    Console.WriteLine("Oops...better test first.");
                }
                order.OrderStatus = test;
                context.SaveChanges();
                order.OrderStatus = ship;
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                foreach (var order in context.Orders)
                {
                    Console.WriteLine("Order {0} [{1}], status = {2}", order.OrderId.ToString(), order.Description, order.OrderStatus.Status);
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
Exemplo n.º 15
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var ordered = context.Lookups.OfType <OrderStatus>().First(s => s.Value == "Ordered");
                var shipped = context.Lookups.OfType <OrderStatus>().First(s => s.Value == "Shipped");
                var cash    = context.Lookups.OfType <TransactionType>().First(s => s.Value == "Cash");
                var fedex   = context.Lookups.OfType <ShippingType>().First(s => s.Value == "FedEx");
                var order   = new Order {
                    Amount = 99.97M, OrderStatus = shipped, ShippingType = fedex, TransactionType = cash
                };
                context.Orders.AddObject(order);
                order = new Order {
                    Amount = 29.99M, OrderStatus = ordered, ShippingType = fedex, TransactionType = cash
                };
                context.Orders.AddObject(order);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                context.ContextOptions.LazyLoadingEnabled = true;
                Console.WriteLine("Active Orders");
                Console.WriteLine("=============");
                foreach (var order in context.Orders)
                {
                    Console.WriteLine("\nOrder: {0}", order.OrderId.ToString());
                    Console.WriteLine("Amount: {0}", order.Amount.ToString("C"));
                    Console.WriteLine("Status: {0}", order.OrderStatus.Value);
                    Console.WriteLine("Shipping via: {0}", order.ShippingType.Value);
                    Console.WriteLine("Paid by: {0}", order.TransactionType.Value);
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
Exemplo n.º 16
0
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var doc1 = new Doctor {
                    Name = "Joan Meyers"
                };
                var doc2 = new Doctor {
                    Name = "Steven Mills"
                };
                var pat1 = new Patient {
                    Name = "Bill Rivers"
                };
                var pat2 = new Patient {
                    Name = "Susan Stevenson"
                };
                var pat3 = new Patient {
                    Name = "Roland Marcy"
                };
                var app1 = new Appointment {
                    Date = DateTime.Today, Doctor = doc1, Fee = 109.92M, Patient = pat1, Reason = "Checkup"
                };
                var app2 = new Appointment {
                    Date = DateTime.Today, Doctor = doc2, Fee = 129.87M, Patient = pat2, Reason = "Arm Pain"
                };
                var app3 = new Appointment {
                    Date = DateTime.Today, Doctor = doc1, Fee = 99.23M, Patient = pat3, Reason = "Back Pain"
                };
                context.Doctors.AddObject(doc1);
                context.Doctors.AddObject(doc2);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                var doc = context.Doctors.First(o => o.Name == "Joan Meyers");
                if (!doc.Appointments.IsLoaded)
                {
                    doc.Appointments.Load();
                    Console.WriteLine("Dr. {0}'s appointments were lazy loaded.", doc.Name);
                }
                Console.WriteLine("Dr. {0} has {1} appointment(s).", doc.Name, doc.Appointments.Count().ToString());

                foreach (var app in context.Appointments)
                {
                    if (!app.DoctorReference.IsLoaded)
                    {
                        app.DoctorReference.Load();
                        Console.WriteLine("Dr. {0} was lazy loaded.", app.Doctor.Name);
                    }
                    else
                    {
                        Console.WriteLine("Dr. {0} was already loaded.", app.Doctor.Name);
                    }
                }

                Console.WriteLine("There are {0} appointments for Dr. {1}", doc.Appointments.Count().ToString(), doc.Name);
                doc.Appointments.Clear();
                Console.WriteLine("Collection clear()'ed");
                Console.WriteLine("There are now {0} appointments for Dr. {1}", doc.Appointments.Count().ToString(), doc.Name);
                doc.Appointments.Load();
                Console.WriteLine("Collection loaded()'ed");
                Console.WriteLine("There are now {0} appointments for Dr. {1}", doc.Appointments.Count().ToString(), doc.Name);
                doc.Appointments.Load(MergeOption.OverwriteChanges);
                Console.WriteLine("Collection loaded()'ed with MergeOption.OverwriteChanges");
                Console.WriteLine("There are now {0} appointments for Dr. {1}", doc.Appointments.Count().ToString(), doc.Name);
            }

            // demonstrating loading part of the collection then Load()'ing the rest
            using (var context = new EFRecipesEntities())
            {
                // load the first doctor and attach just the first appointment
                var doc = context.Doctors.First(o => o.Name == "Joan Meyers");
                doc.Appointments.Attach(doc.Appointments.CreateSourceQuery().Take(1));
                Console.WriteLine("Dr. {0} has {1} appointments loaded.", doc.Name, doc.Appointments.Count().ToString());

                // when we need all of the remaining appointments, simply Load() them
                doc.Appointments.Load();
                Console.WriteLine("Dr. {0} has {1} appointments loaded.", doc.Name, doc.Appointments.Count().ToString());
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }