コード例 #1
0
        public void TestDomainService_UpdateMemberToDefaultValue()
        {
            TestDomainServices.EF.Catalog service = ServerTestHelper.CreateInitializedDomainService <TestDomainServices.EF.Catalog>(DomainOperationType.Submit);

            DomainServiceDescription serviceDescription = DomainServiceDescription.GetDescription(service.GetType());

            // in the below, non RTO is simulated by leaving ReorderPoint as its default value in the
            // original instance
            AdventureWorksModel.Product currProduct = new AdventureWorksModel.Product {
                ProductID = 1, ReorderPoint = 0, Weight = 0
            };
            AdventureWorksModel.Product origProduct = new AdventureWorksModel.Product {
                ProductID = 1, Weight = 50.0M
            };

            // verify expected test state - this test relies on the below attribute values
            PropertyDescriptor pd = TypeDescriptor.GetProperties(typeof(AdventureWorksModel.Product))["ReorderPoint"];

            Assert.IsNull(pd.Attributes[typeof(RoundtripOriginalAttribute)]);
            pd = TypeDescriptor.GetProperties(typeof(AdventureWorksModel.Product))["Weight"];
            Assert.IsNotNull(pd.Attributes[typeof(RoundtripOriginalAttribute)]);
            pd = TypeDescriptor.GetProperties(typeof(AdventureWorksModel.Product))["SafetyStockLevel"];
            Assert.IsNotNull(pd.Attributes[typeof(ExcludeAttribute)]);

            ObjectContextExtensions.AttachAsModified(service.ObjectContext.Products, currProduct, origProduct);

            // verify the expected property modifications
            ObjectStateEntry stateEntry = service.ObjectContext.ObjectStateManager.GetObjectStateEntry(currProduct);

            string[] modifiedProperties = stateEntry.GetModifiedProperties().ToArray();
            Assert.IsTrue(modifiedProperties.Contains("ReorderPoint"));      // no RTO so this should be modified
            Assert.IsTrue(modifiedProperties.Contains("Weight"));            // RTO so this is picked up by normal value comparison
            Assert.IsFalse(modifiedProperties.Contains("SafetyStockLevel")); // excluded member, so shouldn't be marked modified
            Assert.IsFalse(modifiedProperties.Contains("ProductID"));        // key members shouldn't be marked modified
        }
コード例 #2
0
        public void EFDomainService_AddUnmodifiedScenario()
        {
            // create an updated order with an attached new detail
            NorthwindModel.Order order = new NorthwindModel.Order {
                OrderID = 1, ShipCity = "London"
            };
            NorthwindModel.Order origOrder = new NorthwindModel.Order {
                OrderID = 1, ShipCity = "Paris"
            };
            NorthwindModel.Order_Detail detail = new NorthwindModel.Order_Detail {
                OrderID = 1, ProductID = 1
            };
            NorthwindModel.Order_Detail detail2 = new NorthwindModel.Order_Detail {
                OrderID = 1, ProductID = 2
            };
            order.Order_Details.Add(detail);
            order.Order_Details.Add(detail2);

            // create and initialize the service
            DomainServiceDescription dsd = DomainServiceDescription.GetDescription(typeof(TestDomainServices.EF.Northwind));

            TestDomainServices.EF.Northwind nw  = new TestDomainServices.EF.Northwind();
            DomainServiceContext            dsc = new DomainServiceContext(new MockDataService(new MockUser("mathew")
            {
                IsAuthenticated = true
            }), DomainOperationType.Submit);

            nw.Initialize(dsc);

            // call attach directly - this causes the detail to be attached as unmodified
            order.EntityKey     = nw.ObjectContext.CreateEntityKey("Orders", order);
            origOrder.EntityKey = nw.ObjectContext.CreateEntityKey("Orders", order);
            ObjectContextExtensions.AttachAsModified(nw.ObjectContext.Orders, order, origOrder);

            // now insert the detail, and verify that even though the detail is already
            // attached it can be transitioned to new
            nw.InsertOrderDetail(detail);
            nw.InsertOrderDetail(detail2);

            Assert.AreEqual(System.Data.Entity.EntityState.Modified, order.EntityState);
            Assert.AreEqual(System.Data.Entity.EntityState.Added, detail.EntityState);
            Assert.AreEqual(System.Data.Entity.EntityState.Added, detail2.EntityState);
        }