public void Apply_Changes_Should_Mark_Product_Deleted()
		{
			// Arrange
			var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
			var parent = new Product();
			parent.TrackingState = TrackingState.Deleted;

			// Act
			context.ApplyChanges(parent);

			// Assert
			Assert.AreEqual(EntityState.Deleted, context.Entry(parent).State);
		}
예제 #2
0
        public void Modifying_Parent_Should_Mark_Children_As_Unchanged()
        {
            // Arrange
            var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
            var product = new Product { ProductId = 1, CategoryId = 1, ProductName = "TestProduct" };
            var category = new Category
            {
                CategoryId = 1,
                CategoryName = "TestCategory",
                Products = new List<Product> { product }
            };

            // Act
            context.Entry(category).State = EntityState.Modified;

            // Assert
            Assert.Equal(EntityState.Unchanged, context.Entry(product).State);
        }
예제 #3
0
        public void Adding_Parent_Should_Add_Children()
        {
            // Arrange
            var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
            var product = new Product { ProductName = "TestProduct" };
            var category = new Category
            {
                CategoryName = "TestCategory",
                Products = new List<Product> { product }
            };

            // Act
            context.Entry(category).State = EntityState.Added;
            context.SaveChanges();

            // Assert
            Assert.Equal(EntityState.Unchanged, context.Entry(product).State);
        }
예제 #4
0
        public void Deleting_Parent_Should_Delete_Children()
        {
            // Arrange
            var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
            var product = new Product { ProductName = "TestProduct"};
            var category = new Category
            {
                CategoryName = "TestCategory",
                Products = new List<Product> { product }
            };
            context.Categories.Add(category);
            context.SaveChanges();
            int categoryId = category.CategoryId;
            int productId = product.ProductId;
            context.Entry(product).State = EntityState.Detached;
            context.Entry(category).State = EntityState.Detached;
            product = new Product { ProductId = productId, CategoryId = categoryId, ProductName = "TestProduct" };
            category = new Category
            {
                CategoryId = categoryId,
                CategoryName = "TestCategory",
                Products = new List<Product> { product }
            };

            // Act
            // First mark child and parent as unchanged (to attach)
            context.Entry(product).State = EntityState.Unchanged;
            context.Entry(category).State = EntityState.Unchanged;

            // Then mark child and parent as deleted
            context.Entry(product).State = EntityState.Deleted;
            context.Entry(category).State = EntityState.Deleted;
            context.SaveChanges();

            // Assert
            Assert.Equal(EntityState.Detached, context.Entry(category).State);
            Assert.Equal(EntityState.Detached, context.Entry(product).State);
        }
예제 #5
0
        public void Deleting_Child_From_Modified_Parent_Should_Keep_Parent_Modified()
        {
            // Arrange
            var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
            var product = new Product { ProductName = "TestProduct" };
            var category = new Category
            {
                CategoryName = "TestCategory",
                Products = new List<Product> { product }
            };

            // Act
            context.Entry(category).State = EntityState.Modified;
            context.Entry(product).State = EntityState.Deleted;

            // Assert
            Assert.Equal(EntityState.Modified, context.Entry(category).State);
        }
예제 #6
0
        public void Modifying_Parent_Should_Keep_Children_Unchanged()
        {
            // Arrange
            var context = TestsHelper.CreateNorthwindDbContext(CreateNorthwindDbOptions);
            var product = new Product { ProductName = "TestProduct" };
            var category = new Category
            {
                CategoryName = "TestCategory",
                Products = new List<Product> { product }
            };
            context.Categories.Add(category);
            context.SaveChanges();
            int categoryId = category.CategoryId;
            int productId = product.ProductId;
            context.Entry(product).State = EntityState.Detached;
            context.Entry(category).State = EntityState.Detached;
            product = new Product { ProductId = productId, CategoryId = categoryId, ProductName = "TestProduct" };
            category = new Category
            {
                CategoryId = categoryId,
                CategoryName = "TestCategory_Changed",
                Products = new List<Product> { product }
            };

            // Act
            context.Entry(product).State = EntityState.Unchanged;
            context.Entry(category).State = EntityState.Modified;
            context.SaveChanges();

            // Assert
            Assert.Equal(EntityState.Unchanged, context.Entry(product).State);
        }
        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<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};
        }
예제 #10
0
        public static int[] CreateTestProducts()
        {
            using (var context = CreateNorthwindDbContext(CreateNorthwindDbOptions))
            {
                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 = 20M,
                    Category = category1
                };
                var product4 = new Product
                {
                    ProductName = "Test Product 4",
                    UnitPrice = 20M,
                    Category = category2
                };
                var product5 = new Product
                {
                    ProductName = "Test Product 5",
                    UnitPrice = 20M,
                    Category = category2
                };


                context.Products.Add(product1);
                context.Products.Add(product2);
                context.Products.Add(product3);
                context.Products.Add(product4);
                context.Products.Add(product5);
                context.SaveChanges();
                int[] ids =
                {
                    product1.ProductId,
                    product2.ProductId,
                    product3.ProductId,
                    product4.ProductId,
                    product5.ProductId,
                };
                return ids;
            }
        }