Ejemplo n.º 1
0
        private static void CreateCustomerWithOrder()
        {
            var products = GetProducts();
            var product1 = products[0];
            var product2 = products[1];
            var customer = new Customer
            {
                FirstName     = "Julie",
                LastName      = "Lerman",
                ContactDetail = new ContactDetail
                {
                    TwitterAlias = "julielerman"
                },
                DateOfBirth = DateTime.Now
            };
            var order = new Order
            {
                DestinationLatLong = DbGeography.FromText("POINT(44.292401 -72.968102)"),
                OrderDate          = DateTime.Now,
                OrderSource        = OrderSource.InPerson,
                LineItems          = { new LineItem {
                                           ProductId = product1.ProductId, Quantity = 2
                                       },
                                       new LineItem          {
                                           ProductId = product2.ProductId, Quantity = 1
                                       } }
            };

            customer.Orders.Add(order);
            using (var context = new SalesModelContext())
            {
                context.Customers.Add(customer);
                context.SaveChanges();
            }
        }
Ejemplo n.º 2
0
 private static List <Product> GetProducts()
 {
     using (var context = new SalesModelContext())
     {
         return(context.Products.ToList());
     }
 }
Ejemplo n.º 3
0
 private static void ExplicitLoading()
 {
     using (var context = new SalesModelContext())
     {
         var customer = context.Customers.First(c => c.Orders.Any());
         context.Entry(customer).Collection(c => c.Orders).Load();
         Console.WriteLine("Order count for {0}: {1}",
                           customer.FirstName, customer.Orders.Count);
     }
 }
Ejemplo n.º 4
0
 private static void GetCustomers()
 {
     using (var context = new SalesModelContext())
     {
         var customers = context.Customers.ToList();
         foreach (var customer in customers)
         {
             Console.WriteLine(customer.FirstName);
         }
     }
 }
Ejemplo n.º 5
0
 private static void InsertCustomer()
 {
     using (var ctx = new SalesModelContext())
     {
         var customer = new Customer {
             FirstName = "First", LastName = "Last", DateOfBirth = new DateTime(1980, 1, 5)
         };
         ctx.Customers.Add(customer);
         ctx.SaveChanges();
     }
 }
Ejemplo n.º 6
0
 private static void GetCustomers()
 {
     using (var ctx = new SalesModelContext())
     {
         var customers = ctx.Customers.ToList();
         foreach (var customer in customers)
         {
             Console.WriteLine(customer.FirstName);
         }
     }
 }
Ejemplo n.º 7
0
 private static void LazyLoading()
 {
     using (var context = new SalesModelContext())
     {
         context.Configuration.LazyLoadingEnabled = true;
         var customer = context.Customers.First(c => c.Orders.Any());
         //context.Entry(customer).Collection(c => c.Orders).Load();
         Console.WriteLine("Order count for {0}: {1}",
                           customer.FirstName, customer.Orders.Count);
     }
 }
Ejemplo n.º 8
0
 private static void GetCustomer(int id)
 {
     using (var context = new SalesModelContext())
     {
         var customer = context.Customers.Find(id);
         Console.WriteLine(customer.FirstName);
         Console.WriteLine(customer.DateOfBirth);
     }
     Console.WriteLine("-------------");
     Console.ReadKey();
 }
Ejemplo n.º 9
0
        private static void InsertCustomer()
        {
            var customer = new Customer {
                FirstName = "Julie", LastName = "Lerman", DateOfBirth = DateTime.Now
            };

            using (var context = new SalesModelContext())
            {
                context.Customers.Add(customer);
                context.SaveChanges();
            }
            GetCustomer(customer.CustomerId);
        }
Ejemplo n.º 10
0
        protected override void Seed(SalesModelContext context)
        {
            var customers = new List <Customer> {
                new Customer {
                    FirstName = "Sampson", LastName = "TheNewfie", DateOfBirth = new DateTime(2008, 1, 28)
                },
                new Customer {
                    FirstName = "Yogi", LastName = "TheBear", DateOfBirth = new DateTime(1958, 1, 1)
                }
            };

            context.Customers.AddOrUpdate(
                c => new { c.FirstName, c.LastName }, customers.ToArray());
        }
Ejemplo n.º 11
0
        private static void UpdateCustomer()
        {
            int id;

            using (var context = new SalesModelContext())
            {
                var customer = context.Customers
                               .FirstOrDefault(c => c.FirstName == "Julie");
                id = customer.CustomerId;
                Console.WriteLine(customer.DateOfBirth);
                customer.DateOfBirth = DateTime.Now.AddYears(-25);
                context.SaveChanges();
            }
            GetCustomer(id);
        }
Ejemplo n.º 12
0
 private static void DeleteJulie()
 {
     using (var context = new SalesModelContext())
     {
         var julies = context.Customers
                      .Where(c => c.FirstName == "Julie")
                      .ToList();
         foreach (var customer in julies)
         {
             context.Customers.Remove(customer);
         }
         context.SaveChanges();
     }
     GetCustomers();
 }
Ejemplo n.º 13
0
        private static void EagerLoading()
        {
            using (var context = new SalesModelContext())
            {
                var eagerLoadGraph = context.Customers.Include(c => c.Orders).ToList();

                var eagerLoadGraph2 = context.Customers.Include("Orders").ToList();

                var eagerLoadGraph3 = context.Customers.Include("Orders.LineItems").ToList();

                var eagerLoadGraph4 = context.Customers
                                      .Where(c => c.Orders.Any())
                                      .Include(c => c.Orders.Select(o => o.LineItems.Select(l => l.Product)))
                                      .ToList();
                var customer = eagerLoadGraph4[0];
            }
        }
Ejemplo n.º 14
0
        private static void LoadWithProjection()
        {
            using (var context = new SalesModelContext())
            {
                var customerOrderGraph = context.Customers
                                         .Select(c => new { c, c.Orders })
                                         .ToList();

                var customer = customerOrderGraph[2].c;

                var customerWithFirstOrder =
                    context.Customers
                    .Select(c => new
                {
                    c,
                    FirstOrder = c.Orders.OrderBy(o => o.OrderDate).FirstOrDefault()
                })
                    .ToList();
            }
        }
Ejemplo n.º 15
0
 private static void InsertCustomer()
 {
     using (var ctx = new SalesModelContext())
     {
         var customer = new Customer { FirstName = "First", LastName = "Last", DateOfBirth = new DateTime(1980, 1, 5) };
         ctx.Customers.Add(customer);
         ctx.SaveChanges();
     }
 }
Ejemplo n.º 16
0
        protected override void Seed(SalesModelContext context)
        {
            //this check for Any customers is specific to distributing the demo solution.
            //I've left automatic migrations in place so you'll get the intial database
            //and data. But I don't want the AddOrUpdate to run each time you run the app
            //Once the app has created the database upon first run, you can disable the initialization by commenting out
            //the entire <contexts> section in the console app's app.config file. And then remove the
            //check for any customers so that you don't hit the database at all.

            if (!context.Customers.Any()) //after removing initialization in config file remove this line
            {                             //after removing initialization in config file remove this line
                var customers = new List <Customer>
                {
                    new Customer
                    {
                        FirstName   = "Sampson",
                        LastName    = "TheNewfie",
                        DateOfBirth = new DateTime(2008, 1, 28)
                    },
                    new Customer
                    {
                        FirstName   = "Yogi",
                        LastName    = "TheBear",
                        DateOfBirth = new DateTime(1958, 1, 1)
                    }
                };
                context.Customers.AddOrUpdate(
                    c => new { c.FirstName, c.LastName }, customers.ToArray());

                var dogstuff = new Category()
                {
                    Name = "Dog stuff"
                };
                var bearstuff = new Category()
                {
                    Name = "Bear stuff"
                };
                var products = new List <Product>()
                {
                    new Product()
                    {
                        Name            = "Bone",
                        Description     = "Juicy Dog Bone",
                        Categories      = { dogstuff },
                        ProductionStart = DateTime.Now
                    },
                    new Product()
                    {
                        Name            = "Katie's Bumper Toy",
                        Description     = "Super fun dog toy",
                        Categories      = { dogstuff },
                        ProductionStart = DateTime.Now
                    },
                    new Product()
                    {
                        Name            = "Picnic Basket",
                        Description     = "To carry your food",
                        Categories      = { bearstuff },
                        ProductionStart = DateTime.Now
                    },
                    new Product()
                    {
                        Name            = "Honey",
                        Description     = "For bears who like honey",
                        Categories      = { bearstuff },
                        ProductionStart = DateTime.Now
                    }
                };
                context.Products.AddOrUpdate(p => p.Name, products.ToArray());
            }                        //after removing initialization in config file remove this line
        }