コード例 #1
0
        public static void Main(string[] args)
        {
            var dbContext = new NorthwindEntities();

            var startDate = new DateTime(1995, 10, 10);
            var endDate = new DateTime(1997, 01, 1);
            var region = "RJ";

            var orders = dbContext
                .Orders
                .Where(o => o.OrderDate >= startDate && o.OrderDate <= endDate)
                .Where(o => o.ShipRegion == region)
                .Select(o => new
                {
                    Id = o.OrderID,
                    Quantity = o.Order_Details.FirstOrDefault().Quantity,
                    Region = o.ShipRegion
                })
                .ToList();

            foreach (var order in orders)
            {
                Console.WriteLine("{0}, {1}, {2}", order.Id, order.Quantity, order.Region);
            }
        }
コード例 #2
0
        public static void Main(string[] args)
        {
            using(var db = new NorthwindEntities())
            {
                var firstCategory = db.Categories.Where(cat => cat.CategoryID == 1).FirstOrDefault();

                Console.WriteLine(firstCategory.CategoryName);
            }
        }
コード例 #3
0
        private static void ModifyCustomer()
        {
            using (var dbContext = new NorthwindEntities())
            {
                var luckyCustomer = dbContext.Customers.FirstOrDefault();
                luckyCustomer.Country = "Bulgaria";
                dbContext.SaveChanges();
            }

            Console.WriteLine("The customer was modified successfully");
        }
コード例 #4
0
        private static void DeleteCustomer()
        {
            using (var dbContext = new NorthwindEntities())
            {
                var unluckyCustomer = dbContext.Customers.Where(c => c.CustomerID == "100").FirstOrDefault();
                dbContext.Customers.Remove(unluckyCustomer);
                dbContext.SaveChanges();
            }

            Console.WriteLine("The customer was deleted successfully");
        }
コード例 #5
0
        public static void Main(string[] args)
        {
            using (var dbContext = new NorthwindEntities())
            {
                var customers = dbContext
                    .Customers
                    .Where(c => c.Orders.Any(x => x.OrderDate != null && (x.OrderDate.Value.Year == 1997 && x.ShipCountry == "Canada")))
                    .ToList();

                foreach (var customer in customers)
                {
                    Console.WriteLine("{0}, {1}", customer.ContactName, customer.CompanyName);
                }
            }
        }
コード例 #6
0
        private static void InsertCustomer()
        {
            using (var dbContext = new NorthwindEntities())
            {
                dbContext.Customers.Add(new Customer()
                {
                    CompanyName = "Fake company",
                    CustomerID = "100"
                });

                dbContext.SaveChanges();
            }

            Console.WriteLine("The customer was added successfully");
        }
コード例 #7
0
        public static void Main(string[] args)
        {
            var dbContext = new NorthwindEntities();

            var randomEmployee = dbContext
                .Employees
                .Select(e => new
                {
                    e.FirstName,
                    e.Territories
                })
                .FirstOrDefault();

            Console.WriteLine(randomEmployee.FirstName);

            foreach (var teritory in randomEmployee.Territories)
            {
                Console.WriteLine(teritory.TerritoryDescription);
            }
        }
コード例 #8
0
        public static void Main(string[] args)
        {
            var firstDbContext = new NorthwindEntities();
            var secondDbContext = new NorthwindEntities();

            var randomRegion = firstDbContext.Regions.FirstOrDefault();
            var anotherRandomRegion = secondDbContext.Regions.FirstOrDefault();

            randomRegion.RegionDescription = "Changed Description 1";
            anotherRandomRegion.RegionDescription = "changed Description 2";

            firstDbContext.SaveChanges();
            secondDbContext.SaveChanges();

            firstDbContext.Dispose();
            secondDbContext.Dispose();

            var thirdDbContext = new NorthwindEntities();
            var actualRegion = thirdDbContext.Regions.FirstOrDefault();
            Console.WriteLine(actualRegion.RegionDescription);
        }