partial void UpdateProduct(Product instance);
 partial void DeleteProduct(Product instance);
        public void RemoveEventRelayed()
        {
            Product first = new Product();
            Product second = new Product();
            NotifyingCollection<Product> source = new NotifyingCollection<Product>() { first, second };
            PagedEntityCollectionView<Product> pecView = new PagedEntityCollectionView<Product>(source);
            DomainDataSourceView view = new DomainDataSourceView(pecView);

            this.AssertCollectionChanged(
                () => source.Remove(first),
                view,
                new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, first, 0),
                "when removing the first item.");
        }
 partial void InsertProduct(Product instance);
        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);
        }
        public void UncommitedEntityEdits()
        {
            Northwind ctxt = new Northwind(TestURIs.LTS_Northwind);
            Product prod = new Product
            {
                ProductID = 1,
                ProductName = "Cheezy Tots"
            };
            ctxt.EntityContainer.LoadEntities(new Entity[] { prod });

            // start an edit session and calculate
            // changes w/o ending the session
            IEditableObject eo = (IEditableObject)prod;
            eo.BeginEdit();
            prod.ProductName = "Chikn Crisps";
            Assert.IsTrue(prod.HasChanges);
            Assert.IsTrue(prod.IsEditing);
            EntityChangeSet cs = ctxt.EntityContainer.GetChanges();
            Assert.AreEqual(1, cs.ModifiedEntities.Count);

            // however, attempting to call submit will result in
            // an exception
            ExceptionHelper.ExpectInvalidOperationException(delegate
            {
                ctxt.SubmitChanges(TestHelperMethods.DefaultOperationAction, null);
            }, string.Format(Resource.Entity_UncommittedChanges, prod));

            // end the session
            eo.EndEdit();
            Assert.IsFalse(prod.IsEditing);
            cs = ctxt.EntityContainer.GetChanges();
            Assert.AreEqual(1, cs.ModifiedEntities.Count);
        }
 /// <summary>
 /// Invokes the 'DiscontinueProduct' method of the specified <see cref="Product"/> entity.
 /// </summary>
 /// <param name="product">The <see cref="Product"/> entity instance.</param>
 public void DiscontinueProduct(Product product)
 {
     product.DiscontinueProduct();
 }
        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));
        }
 private void DetachProducts(Product entity)
 {
     entity.Category = null;
 }
 private bool FilterProducts(Product entity)
 {
     return (entity.CategoryID == this.CategoryID);
 }
 private void AttachProducts(Product entity)
 {
     entity.Category = this;
 }
 private bool FilterProduct(Product entity)
 {
     return (entity.ProductID == this.ProductID);
 }
        public void AddEventRelayed()
        {
            Product first = new Product();
            Product second = new Product();
            NotifyingCollection<Product> source = new NotifyingCollection<Product>() { first, second };
            PagedEntityCollectionView<Product> pecView = new PagedEntityCollectionView<Product>(source);
            DomainDataSourceView view = new DomainDataSourceView(pecView);

            Product third = new Product();

            this.AssertCollectionChanged(
                () => source.Add(third),
                view,
                new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, third, 2),
                "when adding the third item.");
        }
        public void Bug635474_IsSubmittingStateManagement()
        {
            Northwind nw = new Northwind(TestURIs.LTS_Northwind);
            Product prod = new Product
            {
                ProductID = 1,
                ProductName = "Tasty O's"
            };
            nw.Products.Attach(prod);

            // verify that IsSubmitting is reset when the submit
            // is cancelled
            prod.ProductName += "x";
            SubmitOperation so = nw.SubmitChanges(TestHelperMethods.DefaultOperationAction, null);
            so.Cancel();
            Assert.IsFalse(nw.IsSubmitting);
        }
		private void attach_Products(Product entity)
		{
			this.SendPropertyChanging();
			entity.Category = this;
		}
        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));
        }
		private void detach_Products(Product entity)
		{
			this.SendPropertyChanging();
			entity.Category = null;
		}
        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));
        }
		private void attach_Products(Product entity)
		{
			this.SendPropertyChanging();
			entity.Supplier = this;
		}
        public void IdentityCacheCleanup()
        {
            NorthwindEntityContainer ec = new NorthwindEntityContainer();
            ec.LoadEntities(new Entity[] { new Product { ProductID = 1 }, new Product { ProductID = 2 }, new Product { ProductID = 3 } });
            EntitySet<Product> productSet = ec.GetEntitySet<Product>();

            // Delete an entity
            Product prod = productSet.First();
            int key = prod.ProductID;
            productSet.Remove(prod);
            ((IChangeTracking)ec).AcceptChanges();
            // After the delete, the entity is moved back into the default state
            Assert.AreEqual(EntityState.Detached, prod.EntityState);

            // After it has been deleted, we should be able to add
            // a new entity with the same key
            Product newProduct = new Product
            {
                ProductID = key
            };
            productSet.Add(newProduct);
            Assert.AreEqual(EntityState.New, newProduct.EntityState);
            ((IChangeTracking)ec).AcceptChanges();
            Product requeriedProduct = productSet.Single(p => p.ProductID == key);
            Assert.AreSame(newProduct, requeriedProduct);  // make sure instances are same

            // Bug 526544 repro case - delete and submit, then attempt
            // to re-add
            prod = productSet.First();
            key = prod.ProductID;
            productSet.Remove(prod);
            ((IChangeTracking)ec).AcceptChanges();
            productSet.Add(prod);
            Assert.AreEqual(EntityState.New, prod.EntityState);
            ((IChangeTracking)ec).AcceptChanges();

            // verify that when an entity is Detached, it is removed from ID cache
            // after the detach, attaching an entity with the same key should succeed
            prod = productSet.First();
            key = prod.ProductID;
            productSet.Detach(prod);
            newProduct = new Product
            {
                ProductID = key
            };
            productSet.Attach(newProduct);
            requeriedProduct = productSet.Single(p => p.ProductID == key);
            Assert.AreSame(newProduct, requeriedProduct);  // make sure instances are same
        }
		private void detach_Products(Product entity)
		{
			this.SendPropertyChanging();
			entity.Supplier = null;
		}
        public void TestAssociations_UpdatedReferencingNew()
        {
            NorthwindEntityContainer ec = new NorthwindEntityContainer();
            Product p1 = new Product { ProductID = 1, CategoryID = 1 };
            Product p2 = new Product { ProductID = 2, CategoryID = 2 };
            Category c1 = new Category { CategoryID = 1 };
            Category c2 = new Category { CategoryID = 2 };
            ec.LoadEntities(new Entity[] { p1, p2, c1, c2 });

            // take two existing parents (the FK side of the association)
            // access their existing children
            Category prevCat = p1.Category;
            Assert.IsNotNull(prevCat);
            prevCat = p2.Category;
            Assert.IsNotNull(prevCat);

            // create two new children
            Category newCat1 = new Category { CategoryID = 3 };
            Category newCat2 = new Category { CategoryID = 4 };

            // assign the two new children
            p1.Category = newCat1;
            p2.Category = newCat2;

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

            List<ChangeSetEntry> entries = ChangeSetBuilder.Build(cs);
            ChangeSetEntry entry = entries.Single(p => p.Entity == p1);

            // the bug was that we weren't populating the association map in this
            // scenario since previously we required BOTH parent and child to be new.
            // We've relaxed that to ensure that if the child is new, the association
            // shows up in the map.
            Assert.IsNotNull(entry.Associations);
            int[] ids = entry.Associations["Category"];
            Category referenced = (Category)entries.Single(p => p.Id == ids.Single()).Entity;
            Assert.AreSame(newCat1, referenced);
        }
        public void ResetEventRelayed()
        {
            Product first = new Product();
            Product second = new Product();
            NotifyingCollection<Product> source = new NotifyingCollection<Product>() { first, second };
            PagedEntityCollectionView<Product> pecView = new PagedEntityCollectionView<Product>(source);
            DomainDataSourceView view = new DomainDataSourceView(pecView);

            this.AssertCollectionChanged(
                () => source.Reset(),
                view,
                new NotifyCollectionChangedEventArgs[]
                {
                    new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset),
                    new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset), // Bug 706239
                },
                "when resetting the collection.");
        }