Exemplo n.º 1
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.º 2
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.º 3
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.º 4
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();
        }