static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var tea = new Product {
                    ProductName = "Green Tea", UnitPrice = 1.09M
                };
                var coffee = new Product {
                    ProductName = "Colombian Coffee", UnitPrice = 2.15M
                };
                var customer = new Customer {
                    ContactName = "Karen Marlowe"
                };
                var order1 = new Order {
                    OrderDate = DateTime.Parse("4/19/10")
                };
                order1.OrderDetails.Add(new OrderDetail {
                    Product = tea, Quantity = 4, UnitPrice = 1.00M
                });
                order1.OrderDetails.Add(new OrderDetail {
                    Product = coffee, Quantity = 3, UnitPrice = 2.15M
                });
                customer.Orders.Add(order1);
                context.Customers.AddObject(customer);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                var query = context.Customers.Include("Orders.OrderDetails.Product");
                foreach (var customer in query)
                {
                    Console.WriteLine("Orders for {0}", customer.ContactName);
                    foreach (var order in customer.Orders)
                    {
                        Console.WriteLine("--Order Date: {0}--", order.OrderDate.ToShortDateString());
                        foreach (var detail in order.OrderDetails)
                        {
                            Console.WriteLine("\t{0}, {1} units at {2} each, unit discount: {3}",
                                              detail.Product.ProductName,
                                              detail.Quantity.ToString(),
                                              detail.UnitPrice.ToString("C"),
                                              (detail.Product.UnitPrice - detail.UnitPrice).ToString("C"));
                        }
                    }
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var product1 = new Product {
                    SKU = "CAMP-136", ShortDesription = "High country camping tent", Description = "Use this tent on your next high country adventure.", UnitPrice = 199.95M
                };
                context.Products.AddObject(product1);
                context.SaveChanges();
                Console.WriteLine("Inserted Product {0}: {1}", product1.SKU, product1.ShortDesription);
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var org = new Organizer {
                    Name = "Community Charity"
                };
                var evt = new Event {
                    Name = "Fundraiser"
                };
                org.Events.Add(evt);
                context.Organizers.AddObject(org);
                org = new Organizer {
                    Name = "Boy Scouts"
                };
                evt = new Event {
                    Name = "Eagle Scout Dinner"
                };
                org.Events.Add(evt);
                context.Organizers.AddObject(org);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                var evsorg1 = from ev in context.Events
                              from organizer in ev.Organizers
                              select new { ev.EventId, organizer.OrganizerId };
                Console.WriteLine("Using nested from clauses...");
                foreach (var pair in evsorg1)
                {
                    Console.WriteLine("EventId {0}, OrganizerId {1}", pair.EventId.ToString(), pair.OrganizerId.ToString());
                }

                var evsorg2 = context.Events.SelectMany(e => e.Organizers, (ev, org) => new { ev.EventId, org.OrganizerId });
                Console.WriteLine("\nUsing SelectManay()");
                foreach (var pair in evsorg2)
                {
                    Console.WriteLine("EventId {0}, OrganizerId {1}", pair.EventId.ToString(), pair.OrganizerId.ToString());
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
Esempio n. 4
0
        public Payment InsertPayment()
        {
            using (var context = new EFRecipesEntities())
            {
                // delete previous text data
                context.ExecuteStoreCommand("delete from chapter9.payment");
                context.ExecuteStoreCommand("delete from chapter9.invoice");

                var payment = new Payment {
                    Amount = 99.95M, Invoice = new Invoice {
                        Description = "Auto Repair"
                    }
                };
                context.Payments.AddObject(payment);
                context.SaveChanges();
                return(payment);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            using (var context = new EFRecipesEntities())
            {
                // delete any previous data we might have
                context.ExecuteStoreCommand("delete from chapter4.customer");

                // insert some data
                context.Customers.AddObject(new Customer {
                    Name = "Robin Rosen", City = "Olathe", State = "KS"
                });
                context.Customers.AddObject(new Customer {
                    Name = "John Wise", City = "Springtown", State = "TX"
                });
                context.Customers.AddObject(new Customer {
                    Name = "Karen Carter", City = "Raytown", State = "MO"
                });
                context.SaveChanges();
            }
        }
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                context.Products.AddObject(new Product {
                    Name = "High Country Backpacking Tent", UnitPrice = 199.95M
                });
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                // get the product
                var product = context.Products.SingleOrDefault();
                Console.WriteLine("{0} Unit Price: {1}", product.Name, product.UnitPrice.ToString("C"));

                // delete out of band
                context.ExecuteStoreCommand(@"update chapter14.product set unitprice = 229.95 where productId = @p0", product.ProductId);

                // update the product the via the model
                product.UnitPrice = 239.95M;
                Console.WriteLine("Changing {0}'s Unit Price to: {1}", product.Name, product.UnitPrice.ToString("C"));

                try
                {
                    context.SaveChanges();
                }
                catch (OptimisticConcurrencyException ex)
                {
                    Console.WriteLine("Concurrency Exception! {0}", ex.Message);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Exception! {0}", ex.Message);
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var person = new Person()
                {
                    FirstName = "Robert", MiddleName = "Allen", LastName = "Doe", PhoneNumber = "867-5309"
                };
                context.People.AddObject(person);
                person = new Person()
                {
                    FirstName = "John", MiddleName = "K.", LastName = "Smith", PhoneNumber = "824-3031"
                };
                context.People.AddObject(person);
                person = new Person()
                {
                    FirstName = "Billy", MiddleName = "Albert", LastName = "Minor", PhoneNumber = "907-2212"
                };
                context.People.AddObject(person);
                person = new Person()
                {
                    FirstName = "Kathy", MiddleName = "Anne", LastName = "Ryan", PhoneNumber = "722-0038"
                };
                context.People.AddObject(person);

                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                foreach (var person in context.People)
                {
                    System.Console.WriteLine("{0} {1} {2}, Phone: {3}", person.FirstName, person.MiddleName, person.LastName, person.PhoneNumber);
                }
            }

            System.Console.WriteLine("Press <enter> to continue...");
            System.Console.ReadLine();
        }
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var acc1 = new Account {
                    Balance = 99.34M
                };
                var con1 = new Contact {
                    Name = "Stacy Jones", Phone = "867-5301"
                };
                var cus1 = new Customer {
                    Name = "Bill Waters", Phone = "907-2212", Account = acc1
                };
                context.Contacts.AddObject(con1);
                context.Contacts.AddObject(cus1);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                context.ContextOptions.LazyLoadingEnabled = true;
                Console.WriteLine("All Contacts");
                Console.WriteLine("============");
                foreach (var contact in context.Contacts)
                {
                    Console.WriteLine("{0} {1}", contact.Name, contact.Phone);
                }

                Console.WriteLine("Just Customers");
                foreach (var contact in context.Contacts.OfType <Customer>())
                {
                    Console.WriteLine("\t{0} {1} (Balance: {2})", contact.Name, contact.Phone, contact.Account.Balance.ToString("C"));
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var c1 = new Customer {
                    Name = "Robin Steele", Company = "GoShopNow.com", ContactTitle = "CEO"
                };
                var c2 = new Customer {
                    Name = "Orin Torrey", Company = "GoShopNow.com", ContactTitle = "Sales Manager"
                };
                var c3 = new Customer {
                    Name = "Robert Lancaster", Company = "GoShopNow.com", ContactTitle = "Sales Manager"
                };
                var c4 = new Customer {
                    Name = "Julie Stevens", Company = "GoShopNow.com", ContactTitle = "Sales Manager"
                };
                context.Customers.AddObject(c1);
                context.Customers.AddObject(c2);
                context.Customers.AddObject(c3);
                context.Customers.AddObject(c4);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                var allCustomers = context.GetCustomers("GoShopNow.com", "Sales Manager");
                Console.WriteLine("Customers that are Sales Managers at GoShopNow.com");
                foreach (var c in allCustomers)
                {
                    Console.WriteLine("Customer: {0}", c.Name);
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
        static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var web = new CustomerType {
                    Description = "Web Customer", CustomerTypeId = 1
                };
                var retail = new CustomerType {
                    Description = "Retail Customer", CustomerTypeId = 2
                };
                var customer = new Customer {
                    Name = "Joan Smith", CustomerType = web
                };
                customer.CustomerEmails.Add(new CustomerEmail {
                    Email = "*****@*****.**"
                });
                customer.CustomerEmails.Add(new CustomerEmail {
                    Email = "*****@*****.**"
                });
                context.Customers.AddObject(customer);
                customer = new Customer {
                    Name = "Bill Meyers", CustomerType = retail
                };
                customer.CustomerEmails.Add(new CustomerEmail {
                    Email = "*****@*****.**"
                });
                context.Customers.AddObject(customer);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                var customers = context.Customers.Include("CustomerType").Include("CustomerEmails");
                Console.WriteLine("Customers");
                Console.WriteLine("=========");
                foreach (var customer in customers)
                {
                    Console.WriteLine("{0} is a {1}, email address(es)", customer.Name, customer.CustomerType.Description);
                    foreach (var email in customer.CustomerEmails)
                    {
                        Console.WriteLine("\t{0}", email.Email);
                    }
                }
            }

            using (var context = new EFRecipesEntities())
            {
                var customTypes = context.CustomerTypes.Include("Customers.CustomerEmails");
                Console.WriteLine("\nCustomers by Type");
                Console.WriteLine("=================");
                foreach (var customerType in customTypes)
                {
                    Console.WriteLine("Customer type: {0}", customerType.Description);
                    foreach (var customer in customerType.Customers)
                    {
                        Console.WriteLine("{0}", customer.Name);
                        foreach (var email in customer.CustomerEmails)
                        {
                            Console.WriteLine("\t{0}", email.Email);
                        }
                    }
                }
            }

            Console.WriteLine("Press <enter> to continue...");
            Console.ReadLine();
        }
Esempio n. 11
0
        private static void RunExample()
        {
            using (var context = new EFRecipesEntities())
            {
                var web = new CustomerType {
                    Description = "Web Customer", CustomerTypeId = 1
                };
                var retail = new CustomerType {
                    Description = "Retail Customer", CustomerTypeId = 2
                };
                var customer = new Customer {
                    Name = "Joan Smith", CustomerType = web
                };
                customer.CustomerEmails.Add(new CustomerEmail {
                    Email = "*****@*****.**"
                });
                customer.CustomerEmails.Add(new CustomerEmail {
                    Email = "*****@*****.**"
                });
                context.Customers.Add(customer);
                customer = new Customer {
                    Name = "Bill Meyers", CustomerType = retail
                };
                customer.CustomerEmails.Add(new CustomerEmail {
                    Email = "*****@*****.**"
                });
                context.Customers.Add(customer);
                context.SaveChanges();
            }

            using (var context = new EFRecipesEntities())
            {
                var customers = context.Customers;
                Console.WriteLine("Customers");
                Console.WriteLine("=========");

                // Only information from the Customer entity is requested
                foreach (var customer in customers)
                {
                    Console.WriteLine("Customer name is {0}", customer.Name);
                }

                // Now, application is requesting information from the related entities, CustomerType
                // and CustomerEmail, resulting in Entity Framework generating separate queries to each
                // entity object in order to obtain the requested information.
                foreach (var customer in customers)
                {
                    Console.WriteLine("{0} is a {1}, email address(es)", customer.Name,
                                      customer.CustomerType.Description);
                    foreach (var email in customer.CustomerEmails)
                    {
                        Console.WriteLine("\t{0}", email.Email);
                    }
                }

                // Extra credit:
                // If you enable SQL Server Profiler, the following query will not requery the database
                // for related data. Instead, it will return the in-memory data from the prior
                // query.
                foreach (var customer in customers)
                {
                    Console.WriteLine("{0} is a {1}, email address(es)", customer.Name,
                                      customer.CustomerType.Description);
                    foreach (var email in customer.CustomerEmails)
                    {
                        Console.WriteLine("\t{0}", email.Email);
                    }
                }
            }

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