public void DbPropertyValues_ToObject_for_an_entity_works_under_partial_trust()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");

                var buildingClone = (Building)context.Entry(building).CurrentValues.ToObject();

                Assert.Equal("Building One", buildingClone.Name);
            }
        }
        public void DbPropertyValues_ToObject_for_a_complex_type_works_under_partial_trust()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");

                var addressClone =
                    (Address)context.Entry(building).CurrentValues.GetValue<DbPropertyValues>("Address").ToObject();

                Assert.Equal("Redmond", addressClone.City);
            }
        }
        private void DbPropertyEntryTest(EntityState state, Action<DbEntityEntry<Building>> test)
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var entry = context.Entry(new Building
                                          {
                                              Name = "Building 18",
                                              Address = new Address
                                                        {
                                                            Street = "1 Microsoft Way",
                                                            City = "Redmond",
                                                            State = "WA",
                                                            ZipCode = "98052",
                                                            County = "KING",
                                                            SiteInfo = new SiteInfo { Zone = 2, Environment = "Clean" }
                                                        },
                                              NotInModel = "NotInModel",
                                          });

                entry.State = state;

                test(entry);
            }
        }
        public void IsPropertyChanged_returns_true_for_scalar_property_that_is_changed_and_marked_as_modified()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);
                var stateEntry = GetObjectContext(context).ObjectStateManager.GetObjectStateEntry(building);

                entry.Property(b => b.Name).CurrentValue = "Oops I Did It Again!";

                Assert.True(entry.Property(b => b.Name).IsModified);
                Assert.True(stateEntry.IsPropertyChanged("Name"));

                building.Name = "Building Two";
                context.ChangeTracker.DetectChanges();
                building.Name = "Building One";
                context.ChangeTracker.DetectChanges();


                Assert.True(entry.Property(b => b.Name).IsModified);
                Assert.False(stateEntry.IsPropertyChanged("Name"));
            }
        }
        private void IsPropertyChangedTest(Action<Building, DbEntityEntry<Building>, ObjectStateEntry> test)
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);
                var stateEntry = GetObjectContext(context).ObjectStateManager.GetObjectStateEntry(building);

                test(building, entry, stateEntry);
            }
        }
Example #6
0
        public void Setting_a_nested_complex_property_current_value_onto_a_complex_property_that_is_null_for_a_Detached_entity_throws()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var entry = context.Entry(CreateBuilding(nullAddress: true));

                Assert.Throws<InvalidOperationException>(
                    () =>
                    entry.ComplexProperty(b => b.Address).Property(a => a.SiteInfo).CurrentValue = CreateSiteInfo()).
                    ValidateMessage(
                        "DbPropertyValues_CannotSetPropertyOnNullCurrentValue", typeof(SiteInfo).Name,
                        typeof(Address).Name);
            }
        }
        private void ValidateBuildingAndNameNotModified(AdvancedPatternsMasterContext context, Building building)
        {
            var entry = context.Entry(building);

            Assert.False(entry.Property(b => b.Name).IsModified);
            Assert.False(entry.ComplexProperty(b => b.Address).IsModified);
            Assert.False(entry.ComplexProperty(b => b.Address).Property(a => a.Street).IsModified);
            Assert.False(entry.ComplexProperty(b => b.Address).Property(a => a.City).IsModified);
            Assert.False(entry.ComplexProperty(b => b.Address).Property(a => a.State).IsModified);
            Assert.False(entry.ComplexProperty(b => b.Address).Property(a => a.ZipCode).IsModified);
            Assert.False(entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).IsModified);
            Assert.False(
                entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Environment).
                    IsModified);
            Assert.False(
                entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Zone).IsModified);

            Assert.Equal("Building One", entry.Property(b => b.Name).CurrentValue);
            Assert.Equal("100 Work St", entry.ComplexProperty(b => b.Address).Property(a => a.Street).CurrentValue);
            Assert.Equal("Redmond", entry.ComplexProperty(b => b.Address).Property(a => a.City).CurrentValue);
            Assert.Equal("WA", entry.ComplexProperty(b => b.Address).Property(a => a.State).CurrentValue);
            Assert.Equal("98052", entry.ComplexProperty(b => b.Address).Property(a => a.ZipCode).CurrentValue);
            Assert.Equal("Clean",
                         entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(
                             i => i.Environment).CurrentValue);
            Assert.Equal(1,
                         entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Zone).
                             CurrentValue);

            var objectContext = GetObjectContext(context);
            var modified = objectContext.ObjectStateManager.GetObjectStateEntry(building).GetModifiedProperties();
            Assert.False(modified.Contains("Name"));
            Assert.False(modified.Contains("Address"));
        }
        private void ValidateModifiedProperties(AdvancedPatternsMasterContext context, Building building)
        {
            var entry = context.Entry(building);

            Assert.Equal(EntityState.Modified, entry.State);
            Assert.True(entry.Property(b => b.Name).IsModified);
            Assert.True(entry.ComplexProperty(b => b.Address).IsModified);
            Assert.False(entry.Property(b => b.Value).IsModified);
            Assert.True(entry.ComplexProperty(b => b.Address).Property(a => a.Street).IsModified);
            Assert.True(entry.ComplexProperty(b => b.Address).Property(a => a.City).IsModified);
            Assert.True(entry.ComplexProperty(b => b.Address).Property(a => a.State).IsModified);
            Assert.True(entry.ComplexProperty(b => b.Address).Property(a => a.ZipCode).IsModified);
            Assert.True(entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).IsModified);
            Assert.True(
                entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Environment).
                    IsModified);
            Assert.True(
                entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Zone).IsModified);

            var objectContext = GetObjectContext(context);
            var modified = objectContext.ObjectStateManager.GetObjectStateEntry(building).GetModifiedProperties();
            Assert.Equal(2, modified.Count());
            Assert.True(modified.Contains("Name"));
            Assert.True(modified.Contains("Address"));
        }
        private void TestPropertyValuePositiveForState(Func<DbPropertyEntry<Building, string>, string> getValue,
                                                       Action<DbPropertyEntry<Building, string>, string> setValue,
                                                       EntityState state)
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);
                var propEntry = entry.Property(b => b.Name);
                entry.State = state;

                Assert.Equal("Building One", getValue(propEntry));

                setValue(propEntry, "New Building");
                Assert.Equal("New Building", getValue(propEntry));
            }
        }
        public void Rejecting_changes_to_a_complex_property_creates_a_new_complex_object_which_is_then_not_detected_as_changed_by_future_DetectChanges()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);
                var originalAddress = building.Address;
                var originalSiteInfo = originalAddress.SiteInfo;
                var newAddress = CreateNewAddress();
                var newSiteInfo = newAddress.SiteInfo;

                entry.ComplexProperty(b => b.Address).CurrentValue = newAddress;
                Assert.True(entry.Property(b => b.Address).IsModified);

                entry.ComplexProperty(b => b.Address).IsModified = false;

                ValidateBuildingAndNameNotModified(context, building);

                Assert.NotSame(newAddress, building.Address);
                Assert.NotSame(newSiteInfo, building.Address.SiteInfo);

                Assert.NotSame(originalAddress, building.Address);
                Assert.NotSame(originalSiteInfo, building.Address.SiteInfo);

                Assert.Equal("300 Main St", newAddress.Street);
                Assert.Equal("Ames", newAddress.City);
                Assert.Equal("IA", newAddress.State);
                Assert.Equal("50010", newAddress.ZipCode);
                Assert.Equal("Contaminated", newAddress.SiteInfo.Environment);
                Assert.Equal(3, newAddress.SiteInfo.Zone);

                context.ChangeTracker.DetectChanges();
                ValidateBuildingAndNameNotModified(context, building);
            }
        }
            Setting_IsModified_to_false_for_a_nested_property_of_a_modified_complex_property_rejects_changes_to_the_top_level_complex_property
            ()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);
                entry.ComplexProperty(b => b.Address).Property(a => a.City).CurrentValue = "Madrid";
                Assert.True(entry.Property(b => b.Address).IsModified);

                entry.ComplexProperty(b => b.Address).Property(a => a.City).IsModified = false;

                ValidateBuildingAndNameNotModified(context, building);
            }
        }
        public void DbPropertyValues_SetValues_for_an_entity_wih_complex_objects_works_under_partial_trust()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");

                var newBuilding = new Building
                                      {
                                          BuildingId = new Guid(building.BuildingId.ToString()),
                                          Name = "Bag End",
                                          Value = building.Value,
                                          Address = new Address
                                                        {
                                                            Street = "The Hill",
                                                            City = "Hobbiton",
                                                            State = "WF",
                                                            ZipCode = "00001",
                                                            SiteInfo = new SiteInfo
                                                                           {
                                                                               Zone = 3,
                                                                               Environment = "Comfortable"
                                                                           }
                                                        },
                                      };

                context.Entry(building).CurrentValues.SetValues(newBuilding);

                Assert.Equal("Bag End", building.Name);
                Assert.Equal("Hobbiton", building.Address.City);
                Assert.Equal("Comfortable", building.Address.SiteInfo.Environment);
            }
        }
Example #13
0
            Calling_DetectChanges_twice_for_nested_complex_type_that_was_null_but_is_no_longer_null_should_work_and_original_value_should_be_null
            ()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var entry = context.Entry(CreateBuilding(nullAddress: false, nullSiteInfo: true));
                entry.State = EntityState.Unchanged;

                context.ChangeTracker.DetectChanges();

                entry.Entity.Address.SiteInfo = CreateSiteInfo();

                context.ChangeTracker.DetectChanges();
                context.ChangeTracker.DetectChanges();

                Assert.NotNull(entry.Property(b => b.Address).OriginalValue);
                Assert.NotNull(entry.Property(b => b.Address).CurrentValue);
                Assert.Equal("Donkey Boulevard", entry.Property(b => b.Address).OriginalValue.Street);
                Assert.Equal("Donkey Boulevard", entry.Property(b => b.Address).CurrentValue.Street);

                Assert.Null(entry.ComplexProperty(b => b.Address).Property(a => a.SiteInfo).OriginalValue);
                Assert.NotNull(entry.ComplexProperty(b => b.Address).Property(a => a.SiteInfo).CurrentValue);
                Assert.Equal(18, entry.ComplexProperty(b => b.Address).Property(a => a.SiteInfo).CurrentValue.Zone);
            }
        }
Example #14
0
            Calling_DetectChanges_twice_for_complex_type_that_is_null_should_work_and_original_value_should_be_null()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var entry = context.Entry(CreateBuilding(nullAddress: true));
                entry.State = EntityState.Unchanged;

                context.ChangeTracker.DetectChanges();
                context.ChangeTracker.DetectChanges();

                Assert.Null(entry.Property(b => b.Address).OriginalValue);
                Assert.Null(entry.Property(b => b.Address).CurrentValue);

                Assert.Null(entry.ComplexProperty(b => b.Address).Property(a => a.SiteInfo).OriginalValue);
                Assert.Null(entry.ComplexProperty(b => b.Address).Property(a => a.SiteInfo).CurrentValue);
            }
        }
 private DbEntityEntry<Building> GetBuildingEntry(AdvancedPatternsMasterContext context)
 {
     var building = new Building();
     context.Buildings.Attach(building);
     return context.Entry(building);
 }
        public void Setting_IsModified_to_false_for_a_modified_property_marks_the_entity_as_Unchanged_if_no_properties_remain_modified()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);
                entry.Property(b => b.Value).CurrentValue = 100.0M;
                entry.Property(b => b.Name).CurrentValue = "Oops I Did It Again!";

                entry.Property(b => b.Value).IsModified = false;
                Assert.Equal(EntityState.Modified, entry.State); // Name is still modified

                entry.Property(b => b.Name).IsModified = false;
                Assert.Equal(EntityState.Unchanged, entry.State); // Nothing is modified
            }
        }
        public void Original_value_cannot_be_read_or_set_for_an_object_in_the_Added_state()
        {
            EntityState state = EntityState.Added;
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);
                var propEntry = entry.Property(b => b.Name);

                entry.State = state;

                Assert.Throws<InvalidOperationException>(() => { var _ = propEntry.OriginalValue; }).ValidateMessage(
                    "DbPropertyValues_CannotGetValuesForState", "OriginalValues", state.ToString());
                Assert.Throws<InvalidOperationException>(() => propEntry.OriginalValue = "").ValidateMessage(
                    "DbPropertyValues_CannotGetValuesForState", "OriginalValues", state.ToString());
            }
        }
        public void Setting_IsModified_to_false_for_a_nested_property_of_a_modified_complex_property_marks_the_entity_as_Unchanged_if_no_properties_remain_modified()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);
                entry.Property(b => b.Value).CurrentValue = 100.0M;
                entry.ComplexProperty(b => b.Address).Property(a => a.City).CurrentValue = "Madrid";

                entry.ComplexProperty(b => b.Address).Property(a => a.City).IsModified = false;
                Assert.False(entry.ComplexProperty(b => b.Address).Property(a => a.City).IsModified);
                Assert.Equal("Redmond", building.Address.City);
                Assert.Equal(EntityState.Modified, entry.State); // Value is still modified

                entry.ComplexProperty(b => b.Address).Property(a => a.City).CurrentValue = "Madrid";
                entry.Property(b => b.Value).IsModified = false;

                entry.ComplexProperty(b => b.Address).Property(a => a.City).IsModified = false;
                Assert.False(entry.ComplexProperty(b => b.Address).Property(a => a.City).IsModified);
                Assert.Equal("Redmond", building.Address.City);
                Assert.Equal(EntityState.Unchanged, entry.State); // Nothing is modified
            }
        }
        public void IsModified_can_be_set_to_true_when_it_is_currently_true()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);
                entry.Property(b => b.Name).IsModified = true;
                entry.ComplexProperty(b => b.Address).IsModified = true;
                entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Zone).IsModified
                    = true;

                entry.Property(b => b.Name).IsModified = true;
                entry.ComplexProperty(b => b.Address).IsModified = true;
                entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Zone).IsModified
                    = true;

                ValidateModifiedProperties(context, building);
            }
        }
            IsModified_stays_true_for_properties_of_a_complex_property_until_changes_are_rejected_to_all_properties()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");

                building.Address.City = "Grimsby";
                building.Address.State = "UK";
                building.Address.SiteInfo.Environment = "Fishy";

                var addressEntry = context.Entry(building).ComplexProperty(b => b.Address);
                AssertStateOfAddressProperties(addressEntry, "Grimsby", "UK", "Fishy", isModified: true);

                addressEntry.Property(a => a.City).IsModified = false;
                AssertStateOfAddressProperties(addressEntry, "Redmond", "UK", "Fishy", isModified: true);

                addressEntry.Property(a => a.State).IsModified = false;
                AssertStateOfAddressProperties(addressEntry, "Redmond", "WA", "Fishy", isModified: true);

                addressEntry.ComplexProperty(a => a.SiteInfo).Property(s => s.Environment).IsModified = false;
                AssertStateOfAddressProperties(addressEntry, "Redmond", "WA", "Clean", isModified: false);
            }
        }
        public void IsModified_can_be_set_to_false_when_it_is_currently_false()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);

                entry.Property(b => b.Name).IsModified = false;
                entry.ComplexProperty(b => b.Address).IsModified = false;
                entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Zone).IsModified
                    = false;

                Assert.False(entry.Property(b => b.Name).IsModified);
                Assert.False(entry.ComplexProperty(b => b.Address).IsModified);
                Assert.False(
                    entry.ComplexProperty(b => b.Address).ComplexProperty(a => a.SiteInfo).Property(i => i.Zone).
                        IsModified);
                Assert.Equal(EntityState.Unchanged, entry.State);

                var objectContext = GetObjectContext(context);
                Assert.Equal(0,
                             objectContext.ObjectStateManager.GetObjectStateEntry(building).GetModifiedProperties().
                                 Count());
            }
        }
        public void IsModified_stays_true_for_properties_of_a_complex_property_until_changes_are_rejected_to_all_properties_even_if_the_instance_has_been_changed()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var addressEntry = context.Entry(building).ComplexProperty(b => b.Address);

                var originalAddress = building.Address;
                var newAddress = CloneAddress(originalAddress);
                newAddress.SiteInfo = originalAddress.SiteInfo; // Keep same nested complex instance

                building.Address = newAddress;
                building.Address.City = "Grimsby";

                addressEntry = context.Entry(building).ComplexProperty(b => b.Address);
                AssertStateOfAddressProperties(addressEntry, "Grimsby", "WA", "Clean", isModified: true);

                addressEntry.Property(a => a.City).IsModified = false;
                AssertStateOfAddressProperties(addressEntry, "Redmond", "WA", "Clean", isModified: false);
            }
        }
        public void Setting_IsModified_to_false_for_a_modified_complex_property_rejects_changes_to_that_property()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var building = context.Buildings.Single(b => b.Name == "Building One");
                var entry = context.Entry(building);
                entry.ComplexProperty(b => b.Address).CurrentValue = CreateNewAddress();
                Assert.True(entry.Property(b => b.Address).IsModified);

                entry.ComplexProperty(b => b.Address).IsModified = false;

                ValidateBuildingAndNameNotModified(context, building);
            }
        }
Example #24
0
            Setting_a_nested_complex_property_original_value_onto_a_complex_property_that_was_originally_null_throws()
        {
            using (var context = new AdvancedPatternsMasterContext())
            {
                var entry = context.Entry(CreateBuilding(nullAddress: true));
                entry.State = EntityState.Unchanged;

                entry.Entity.Address = CreateAddress(nullSiteInfo: true);
                context.ChangeTracker.DetectChanges();

                Assert.Null(entry.Property(b => b.Address).OriginalValue);

                Assert.Throws<InvalidOperationException>(
                    () =>
                    entry.ComplexProperty(b => b.Address).Property(a => a.SiteInfo).OriginalValue = CreateSiteInfo()).
                    ValidateMessage(
                        "DbPropertyValues_CannotSetPropertyOnNullOriginalValue", typeof(SiteInfo).Name,
                        typeof(Address).Name);
            }
        }