public void ChangeSet_DontLoadUnloadedAssociations()
        {
            NorthwindEntityContainer entities = new NorthwindEntityContainer();
            EntitySet<Order> orders = entities.GetEntitySet<Order>();
            EntitySet<Order_Detail> details = entities.GetEntitySet<Order_Detail>();

            // add a few existing entities
            Order order = new Order
            {
                OrderID = 1
            };
            Order_Detail detail = new Order_Detail
            {
                OrderID = 1, ProductID = 1
            };
            entities.LoadEntities(new Entity[] { order, detail });

            // modify both entities
            order.Freight = 5;
            detail.Quantity = 5;

            IEntityRef er = detail.GetEntityRef("Order");
            Assert.IsNull(er);
            IEntityCollection ec = order.Order_Details;
            Assert.IsFalse(ec.HasValues);

            EntityChangeSet cs = entities.GetChanges();
            Assert.AreEqual(2, cs.ModifiedEntities.Count);

            // after computing the changeset, no association members
            // should have been loaded
            er = detail.GetEntityRef("Order");
            Assert.IsNull(er);
            Assert.IsFalse(ec.HasValues);

            // after building the operation list, no association members
            // should have been loaded
            ChangeSetBuilder.Build(cs);
            er = detail.GetEntityRef("Order");
            Assert.IsNull(er);
            Assert.IsFalse(ec.HasValues);
        }
        public void InferredAdd_EntityCollection()
        {
            NorthwindEntityContainer ec = new NorthwindEntityContainer();

            // add a few existing entities
            Order order1 = new Order
            {
                OrderID = 1
            };
            Order order2 = new Order
            {
                OrderID = 2
            };
            Order_Detail detail1 = new Order_Detail
            {
                OrderID = 2, ProductID = 1
            };
            ec.LoadEntities(new Entity[] { order1, order2, detail1 });
            ((IChangeTracking)ec).AcceptChanges();
            Assert.IsFalse(ec.HasChanges);

            // build a detached graph of a new category and 2 products,
            // referenced by a detail
            Order_Detail newDetail1 = new Order_Detail
            {
                OrderID = 1, ProductID = 2
            };
            Category newCategory = new Category
            {
                CategoryID = 1
            };
            Product newProduct1 = new Product
            {
                ProductID = 3
            };
            Product newProduct2 = new Product
            {
                ProductID = 4
            };
            newCategory.Products.Add(newProduct1);
            newCategory.Products.Add(newProduct2);
            newDetail1.Product = newProduct1;

            EntityChangeSet cs = ec.GetChanges();
            Assert.IsTrue(cs.IsEmpty);

            // verify that adding an unattached entity to an EC results in
            // the expected inferred Adds
            order1.Order_Details.Add(newDetail1);
            cs = ec.GetChanges();
            Assert.AreEqual(4, cs.AddedEntities.Count);
            Assert.IsTrue(cs.AddedEntities.Contains(newDetail1));  // the entity added directly
            Assert.IsTrue(cs.AddedEntities.Contains(newProduct1)); // inferred via Detail.Product ER
            Assert.IsTrue(cs.AddedEntities.Contains(newCategory)); // inferred via Product.Category ER
            Assert.IsTrue(cs.AddedEntities.Contains(newProduct2)); // inferred via Category.Products EC

            // verify that inferred Adds can be state transitioned via subsequent
            // calls to Attach
            ec.GetEntitySet<Product>().Attach(newProduct2);
            newProduct2.ProductName += "x";
            cs = ec.GetChanges();
            Assert.AreEqual(3, cs.AddedEntities.Count);
            Assert.AreEqual(1, cs.ModifiedEntities.Count);
            Assert.IsFalse(cs.AddedEntities.Contains(newProduct2));
            Assert.IsTrue(cs.ModifiedEntities.Contains(newProduct2));
        }
        public void EntityCollection_DontInferAddDeletedEntities()
        {
            NorthwindEntityContainer ec = new NorthwindEntityContainer();
            EntitySet<Order> orders = ec.GetEntitySet<Order>();
            EntitySet<Order_Detail> details = ec.GetEntitySet<Order_Detail>();

            // add a few existing entities
            Order order = new Order
            {
                OrderID = 1
            };
            Order_Detail detail = new Order_Detail
            {
                OrderID = 1, ProductID = 1
            };
            details.Attach(detail);
            orders.Attach(order);

            // Verify that EntityCollections don't infer Add deleted
            // entities
            details.Remove(detail);
            order.Order_Details.Add(detail);
            Assert.IsFalse(detail.IsInferred);
            Assert.IsFalse(details.Contains(detail));
        }
        public void Bug619552_DontInferAttachDeletedEntities()
        {
            NorthwindEntityContainer ec = new NorthwindEntityContainer();
            EntitySet<Order> orders = ec.GetEntitySet<Order>();
            EntitySet<Order_Detail> details = ec.GetEntitySet<Order_Detail>();

            // add a few existing entities
            Order order = new Order
            {
                OrderID = 1
            };
            Order_Detail detail = new Order_Detail
            {
                OrderID = 1, ProductID = 1
            };

            // attach
            details.Attach(detail);
            orders.Attach(order);
            EntityChangeSet cs = ec.GetChanges();
            Assert.IsTrue(cs.IsEmpty);

            // remove
            details.Remove(detail);
            orders.Remove(order);
            cs = ec.GetChanges();
            Assert.IsTrue(cs.AddedEntities.Count == 0 && cs.ModifiedEntities.Count == 0 && cs.RemovedEntities.Count == 2);

            // This line should only add the detail as Unmodified
            // Since both entities are known by the container, they
            // are not infer Added
            details.Add(detail);

            // This line adds the order back as Unmodified
            orders.Add(order);

            cs = ec.GetChanges();
            Assert.IsTrue(cs.IsEmpty);
        }
        public void AttachAnInferredEntity()
        {
            NorthwindEntityContainer ec = new NorthwindEntityContainer();
            EntitySet<Order> orders = ec.GetEntitySet<Order>();
            EntitySet<Order_Detail> details = ec.GetEntitySet<Order_Detail>();

            // add a few existing entities
            Order order1 = new Order
            {
                OrderID = 1
            };
            Order_Detail detail1 = new Order_Detail
            {
                Order = order1, ProductID = 1
            };

            // this attaches both entities
            details.Attach(detail1);
            Assert.IsTrue(details.Contains(detail1));
            Assert.IsTrue(orders.Contains(order1));

            // this should work, since the order was inferred
            // it should no-op
            orders.Attach(order1);
        }
 partial void DeleteOrder_Detail(Order_Detail instance);
 private void DetachOrder_Details(Order_Detail entity)
 {
     entity.Order = null;
 }
        public void InferredAdd_RecursiveOnAttach()
        {
            NorthwindEntityContainer ec = new NorthwindEntityContainer();
            EntitySet<Order> ordersSet = ec.GetEntitySet<Order>();

            Order order = new Order
            {
                OrderID = 1
            };
            Order_Detail detail1 = new Order_Detail
            {
                OrderID = 1
            };
            Order_Detail detail2 = new Order_Detail
            {
                OrderID = 1
            };
            Product product1 = new Product
            {
                ProductID = 1
            };
            Product product2 = new Product
            {
                ProductID = 2
            };
            order.Order_Details.Add(detail1);
            order.Order_Details.Add(detail2);
            detail1.Product = product1;
            detail2.Product = product2;

            // when we attach, we expect all reachable unattached entities
            // to be attached
            ordersSet.Attach(order);
            Assert.IsTrue(ec.GetChanges().IsEmpty);
            Assert.IsTrue(ec.GetEntitySet<Order>().Contains(order));
            Assert.IsTrue(ec.GetEntitySet<Order_Detail>().Contains(detail1));
            Assert.IsTrue(ec.GetEntitySet<Order_Detail>().Contains(detail2));
            Assert.IsTrue(ec.GetEntitySet<Product>().Contains(product1));
            Assert.IsTrue(ec.GetEntitySet<Product>().Contains(product2));

            // All attached entities (including the root) can subsequently be transitioned by
            // calling Add/Remove. After the transition, they are no longer "inferred" and cannot
            // be transitioned again
            Assert.IsTrue(product2.IsInferred);
            ec.GetEntitySet<Product>().Remove(product2);
            Assert.IsFalse(product2.IsInferred);
            ec.GetEntitySet<Product>().Add(product2);  // this undoes the remove, making it Unmodified again
            Assert.AreEqual(EntityState.Unmodified, product2.EntityState);

            Assert.IsTrue(product1.IsInferred);
            ec.GetEntitySet<Product>().Add(product1);
            Assert.IsFalse(product1.IsInferred);

            ec.GetEntitySet<Order>().Remove(order);
        }
		private void detach_Order_Details(Order_Detail entity)
		{
			this.SendPropertyChanging();
			entity.Product = null;
		}
		private void attach_Order_Details(Order_Detail entity)
		{
			this.SendPropertyChanging();
			entity.Order = this;
		}
 private bool FilterOrder_Details(Order_Detail entity)
 {
     return (entity.ProductID == this.ProductID);
 }
 private void DetachOrder_Details(Order_Detail entity)
 {
     entity.Product = null;
 }
 private void AttachOrder_Details(Order_Detail entity)
 {
     entity.Product = this;
 }
 private bool FilterOrder_Details(Order_Detail entity)
 {
     return (entity.OrderID == this.OrderID);
 }
        public void InferredAdd_EntityRefs()
        {
            NorthwindEntityContainer ec = new NorthwindEntityContainer();

            // add a few existing entities
            Order order1 = new Order
            {
                OrderID = 1
            };
            Order_Detail detail1 = new Order_Detail
            {
                OrderID = 2, ProductID = 1
            };
            ec.LoadEntities(new Entity[] { order1, detail1 });
            ((IChangeTracking)ec).AcceptChanges();
            Assert.IsFalse(ec.HasChanges);

            // build a detached graph of a new category and 2 products
            Category newCategory = new Category
            {
                CategoryID = 1
            };
            Product newProduct1 = new Product
            {
                ProductID = 3
            };
            Product newProduct2 = new Product
            {
                ProductID = 4
            };
            newCategory.Products.Add(newProduct1);
            newCategory.Products.Add(newProduct2);

            // set the the Product reference on the existing detail to
            // one of the new detached products - we expect the entire
            // graph to be infer added
            EntityChangeSet cs = ec.GetChanges();
            Assert.IsTrue(cs.IsEmpty);
            detail1.Product = newProduct1;
            cs = ec.GetChanges();
            Assert.AreEqual(3, cs.AddedEntities.Count);
            Assert.IsTrue(cs.AddedEntities.Contains(newProduct1)); // the entity set directly
            Assert.IsTrue(cs.AddedEntities.Contains(newCategory)); // inferred via Product.Category ER
            Assert.IsTrue(cs.AddedEntities.Contains(newProduct2)); // inferred via Category.Products EC

            // verify that inferred Adds can be state transitioned via subsequent
            // calls to Attach
            ec.GetEntitySet<Product>().Attach(newProduct2);
            newProduct2.ProductName += "x";
            cs = ec.GetChanges();
            Assert.AreEqual(2, cs.AddedEntities.Count);
            Assert.AreEqual(2, cs.ModifiedEntities.Count);
            Assert.IsFalse(cs.AddedEntities.Contains(newProduct2));
            Assert.IsTrue(cs.ModifiedEntities.Contains(newProduct2));
        }
        public void InferredAdd_RecursiveOnAdd()
        {
            NorthwindEntityContainer ec = new NorthwindEntityContainer();
            EntitySet<Order> ordersSet = ec.GetEntitySet<Order>();

            Order order = new Order
            {
                OrderID = 1
            };
            Order_Detail detail1 = new Order_Detail
            {
                OrderID = 1
            };
            Order_Detail detail2 = new Order_Detail
            {
                OrderID = 1
            };
            Product product1 = new Product
            {
                ProductID = 1
            };
            Product product2 = new Product
            {
                ProductID = 2
            };
            order.Order_Details.Add(detail1);
            order.Order_Details.Add(detail2);
            detail1.Product = product1;
            detail2.Product = product2;

            // when we add, we expect all reachable unattached entities
            // to be infer added
            ordersSet.Add(order);
            EntityChangeSet cs = ec.GetChanges();
            Assert.AreEqual(5, cs.AddedEntities.Count);
            Assert.IsTrue(cs.AddedEntities.Contains(order));
            Assert.IsTrue(cs.AddedEntities.Contains(detail1));
            Assert.IsTrue(cs.AddedEntities.Contains(detail2));
            Assert.IsTrue(cs.AddedEntities.Contains(product1));
            Assert.IsTrue(cs.AddedEntities.Contains(product2));

            // the root entity wasn't infer added, so it can't be Attached
            InvalidOperationException expectedException = null;
            try
            {
                ordersSet.Attach(order);
            }
            catch (InvalidOperationException e)
            {
                expectedException = e;
            }
            Assert.AreEqual(Resource.EntitySet_EntityAlreadyAttached, expectedException.Message);

            // entities that were infer Added can be Attached
            ec.GetEntitySet<Product>().Attach(product1);
            product1.ProductName += "x";
            cs = ec.GetChanges();
            Assert.AreEqual(4, cs.AddedEntities.Count);
            Assert.IsFalse(cs.AddedEntities.Contains(product1));
            Assert.IsTrue(cs.ModifiedEntities.Contains(product1));

            // verify that after an inferred Add has been Attached, it can't be
            // reattached
            expectedException = null;
            try
            {
                ec.GetEntitySet<Product>().Attach(product1);
            }
            catch (InvalidOperationException e)
            {
                expectedException = e;
            }
            Assert.AreEqual(Resource.EntitySet_DuplicateIdentity, expectedException.Message);

            // verify that when changes are accepted, all Inferred state
            // is reset for entities
            cs = ec.GetChanges();
            IEnumerable<Entity> entities = cs.AddedEntities.Concat(cs.ModifiedEntities).Concat(cs.RemovedEntities);
            Assert.AreEqual(3, entities.Count(p => p.IsInferred));
            ((IChangeTracking)ec).AcceptChanges();
            Assert.IsFalse(entities.Any(p => p.IsInferred));
        }
 partial void InsertOrder_Detail(Order_Detail instance);
 partial void UpdateOrder_Detail(Order_Detail instance);
 private void AttachOrder_Details(Order_Detail entity)
 {
     entity.Order = this;
 }