Esempio n. 1
0
 public static NorthwindDbContext CreateNorthwindDbContext(CreateDbOptions createDbOptions)
 {
     // Create new context for all tests
     var context = new NorthwindDbContext(createDbOptions);
     Assert.True(context.Products.Count() >= 0);
     return context;
 }
 private static void EnsureTestTerritory(NorthwindDbContext context, string territoryId)
 {
     var territory = context.Territories
         .SingleOrDefault(t => t.TerritoryId == territoryId);
     if (territory == null)
     {
         territory = new Territory { TerritoryId = territoryId, TerritoryDescription = "Test Territory" };
         context.Territories.Add(territory);
     }
 }
 private static void EnsureTestCustomer(NorthwindDbContext context, string customerId, string territoryId)
 {
     var customer = context.Customers
         .SingleOrDefault(c => c.CustomerId == customerId);
     if (customer == null)
     {
         customer = new Customer
         {
             CustomerId = customerId, 
             CustomerName = "Test Customer " + customerId,
             TerritoryId = territoryId
         };
         context.Customers.Add(customer);
     }
 }
Esempio n. 4
0
        public void Edmx_LoadRelatedEntities_Should_Populate_Multiple_Orders_With_Customer()
        {
            // Create DB usng CodeFirst context
            string providerConnectionString;

            using (var cf = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions))
            {
                providerConnectionString = cf.Database.Connection.ConnectionString;
            }

            // Connect using ModelFirst context
            var asm   = System.Reflection.Assembly.GetExecutingAssembly();
            var paths = from res in asm.GetManifestResourceNames()
                        //where res.Contains("Northwind")
                        where new string[] { ".csdl", ".ssdl", ".msl" }.Contains(System.IO.Path.GetExtension(res))
            select string.Format("res://{0}/{1}", asm.GetName().Name, res);

            var conn = new EntityConnectionStringBuilder();

            conn.Metadata = string.Join("|", paths);
            conn.Provider = "System.Data.SqlClient";
            conn.ProviderConnectionString = providerConnectionString;
            var context = new TrackableEntities.EF.Tests.Contexts.NorthwindDbContext(conn.ToString());

            Database.SetInitializer <TrackableEntities.EF.Tests.Contexts.NorthwindDbContext>(null);

            // Arrange
            var orders = CreateTestOrders(context);

            orders.ForEach(o => o.TrackingState = TrackingState.Added);

            // Act
            context.LoadRelatedEntities(orders);

            // Assert
            Assert.IsFalse(orders.Any(o => o.Customer == null));
            Assert.IsFalse(orders.Any(o => o.Customer.CustomerId != o.CustomerId));
        }
 private static void EnsureTestCustomerSetting(NorthwindDbContext context, string customerId)
 {
     var setting = context.CustomerSettings
         .SingleOrDefault(c => c.CustomerId == customerId);
     if (setting == null)
     {
         setting = new CustomerSetting { CustomerId = customerId, Setting = "Setting1" };
         context.CustomerSettings.Add(setting);
     }
 }
        private List<Product> CreateTestProductsWithProductInfo(NorthwindDbContext context)
        {
            // Create test entities
            var category1 = new Category
            {
                CategoryName = "Test Category 1b"
            };
            var info1 = context.ProductInfos
                .Single(pi => pi.ProductInfoKey1 == ProductInfo1A
                    && pi.ProductInfoKey2 == ProductInfo1B);
            var product1 = new Product
            {
                ProductName = "Test Product 1b",
                UnitPrice = 10M,
                Category = category1,
                ProductInfo = info1
            };

            // Persist entities
            context.Products.Add(product1);
            context.SaveChanges();

            // Detach entities
            var objContext = ((IObjectContextAdapter)context).ObjectContext;
            objContext.Detach(product1);

            // Clear reference properties
            product1.Category = null;
            product1.ProductInfo = null;

            // Return entities
            return new List<Product> { product1 };
        }
        private List<Product> CreateTestProductsWithPromos(NorthwindDbContext context)
        {
            // Create test entities
            var promo1 = new HolidayPromo
            {
                PromoId = 1,
                PromoCode = "THX",
                HolidayName = "Thanksgiving"
            };
            var category1 = new Category
            {
                CategoryName = "Test Category 1a"
            };
            var product1 = new Product
            {
                ProductName = "Test Product 1a",
                UnitPrice = 10M,
                Category = category1,
                HolidayPromo = promo1
            };

            // Persist entities
            context.Products.Add(product1);
            context.SaveChanges();

            // Detach entities
            var objContext = ((IObjectContextAdapter)context).ObjectContext;
            objContext.Detach(product1);

            // Clear reference properties
            product1.Category = null;
            product1.HolidayPromo = null;

            // Return entities
            return new List<Product> { product1 };
        }
        private List<Employee> CreateTestEmployees(NorthwindDbContext context)
        {
            // Create test entities
            var area1 = new Area { AreaName = "Northern" };
            var area2 = new Area { AreaName = "Southern" };
            var territory1 = context.Territories.Single(t => t.TerritoryId == TestTerritoryId1);
            var territory2 = context.Territories.Single(t => t.TerritoryId == TestTerritoryId2);
            var territory3 = context.Territories.Single(t => t.TerritoryId == TestTerritoryId3);
            territory1.Area = area1;
            territory2.Area = area2;
            territory3.Area = area2;
            var employee1 = new Employee
            {
                FirstName = "Test",
                LastName = "Employee1",
                City = "San Fransicso",
                Country = "USA",
                Territories = new List<Territory> { territory1, territory2 }
            };
            var employee2 = new Employee
            {
                FirstName = "Test",
                LastName = "Employee2",
                City = "Sacramento",
                Country = "USA",
                Territories = new List<Territory> { territory2, territory3 }
            };

            // Persist entities
            context.Employees.Add(employee1);
            context.Employees.Add(employee2);
            context.SaveChanges();

            // Detach entities
            var objContext = ((IObjectContextAdapter)context).ObjectContext;
            objContext.Detach(employee1);
            objContext.Detach(employee2);

            // Clear reference properties
            territory1.Area = null;
            territory2.Area = null;
            territory3.Area = null;
            employee1.Territories = new List<Territory> { territory1, territory2 };
            employee2.Territories = new List<Territory> { territory2, territory3 };

            // Return employees
            return new List<Employee> { employee1, employee2 };
        }
        private List<Order> CreateTestOrders(NorthwindDbContext context)
        {
            // Create test entities
            var category1 = new Category
            {
                CategoryName = "Test Category 1"
            };
            var category2 = new Category
            {
                CategoryName = "Test Category 2"
            };
            var product1 = new Product
            {
                ProductName = "Test Product 1",
                UnitPrice = 10M,
                Category = category1
            };
            var product2 = new Product
            {
                ProductName = "Test Product 2",
                UnitPrice = 20M,
                Category = category2
            };
            var product3 = new Product
            {
                ProductName = "Test Product 3",
                UnitPrice = 30M,
                Category = category2
            };
            var customer1 = context.Customers
                .Include(c => c.Territory)
                .Include(c => c.CustomerSetting)
                .Single(c => c.CustomerId == TestCustomerId1);
            var customer2 = context.Customers
                .Include(c => c.Territory)
                .Include(c => c.CustomerSetting)
                .Single(c => c.CustomerId == TestCustomerId1);
            var detail1 = new OrderDetail {Product = product1, Quantity = 11, UnitPrice = 11M};
            var detail2 = new OrderDetail {Product = product2, Quantity = 12, UnitPrice = 12M};
            var detail3 = new OrderDetail {Product = product2, Quantity = 13, UnitPrice = 13M};
            var detail4 = new OrderDetail {Product = product3, Quantity = 14, UnitPrice = 14M};
            var order1 = new Order
            {
                OrderDate = DateTime.Today,
                Customer = customer1,
                OrderDetails = new List<OrderDetail>
                {
                    detail1,
                    detail2,
                }
            };
            var order2 = new Order
            {
                OrderDate = DateTime.Today,
                Customer = customer2,
                OrderDetails = new List<OrderDetail>
                {
                    detail3,
                    detail4,
                }
            };

            // Persist entities
            context.Orders.Add(order1);
            context.Orders.Add(order2);
            context.SaveChanges();

            // Detach entities
            var objContext = ((IObjectContextAdapter) context).ObjectContext;
            objContext.Detach(order1);
            objContext.Detach(order2);

            // Clear reference properties
            product1.Category = null;
            product2.Category = null;
            product3.Category = null;
            customer1.Territory = null;
            customer2.Territory = null;
            customer1.CustomerSetting = null;
            customer2.CustomerSetting = null;
            detail1.Product = null;
            detail2.Product = null;
            detail3.Product = null;
            detail4.Product = null;
            order1.OrderDetails = new List<OrderDetail> { detail1, detail2 };
            order2.OrderDetails = new List<OrderDetail> { detail3, detail4 };

            // Return orders
            return new List<Order> {order1, order2};
        }
 private static void EnsureTestProductInfo(NorthwindDbContext context, int productInfo1, int productInfo2)
 {
     var info = context.ProductInfos
         .SingleOrDefault(pi => pi.ProductInfoKey1 == productInfo1
             && pi.ProductInfoKey2 == productInfo2);
     if (info == null)
     {
         info = new ProductInfo
         {
             ProductInfoKey1 = productInfo1,
             ProductInfoKey2 = productInfo2,
             Info = "Test Product Info"
         };
         context.ProductInfos.Add(info);
     }
 }
        public void Edmx_LoadRelatedEntities_Should_Populate_Multiple_Orders_With_Customer()
        {
            // Create DB usng CodeFirst context
            string providerConnectionString;
            using (var cf = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions))
            {
                providerConnectionString = cf.Database.Connection.ConnectionString;
            }

            // Connect using ModelFirst context
            var asm = System.Reflection.Assembly.GetExecutingAssembly();
            var paths = from res in asm.GetManifestResourceNames()
                        //where res.Contains("Northwind")
                        where new string[] { ".csdl", ".ssdl", ".msl" }.Contains(System.IO.Path.GetExtension(res))
                        select string.Format("res://{0}/{1}", asm.GetName().Name, res);

            var conn = new EntityConnectionStringBuilder();
            conn.Metadata = string.Join("|", paths);
            conn.Provider = "System.Data.SqlClient";
            conn.ProviderConnectionString = providerConnectionString;
            var context = new TrackableEntities.EF.Tests.Contexts.NorthwindDbContext(conn.ToString());
            Database.SetInitializer<TrackableEntities.EF.Tests.Contexts.NorthwindDbContext>(null);

            // Arrange
            var orders = CreateTestOrders(context);
            orders.ForEach(o => o.TrackingState = TrackingState.Added);

            // Act
            context.LoadRelatedEntities(orders);

            // Assert
            Assert.IsFalse(orders.Any(o => o.Customer == null));
            Assert.IsFalse(orders.Any(o => o.Customer.CustomerId != o.CustomerId));
        }