diff --git a/src/EFCore/ChangeTracking/NavigationEntry.cs b/src/EFCore/ChangeTracking/NavigationEntry.cs index 7c745755ec6..f6e1694e9da 100644 --- a/src/EFCore/ChangeTracking/NavigationEntry.cs +++ b/src/EFCore/ChangeTracking/NavigationEntry.cs @@ -260,7 +260,7 @@ private void SetFkPropertiesModified(InternalEntityEntry internalEntityEntry, bo if (anyNonPk && !property.IsPrimaryKey()) { - internalEntityEntry.SetPropertyModified(property, isModified: modified, acceptChanges: true); + internalEntityEntry.SetPropertyModified(property, isModified: modified, acceptChanges: false); } } } diff --git a/test/EFCore.Tests/ChangeTracking/CollectionEntryTest.cs b/test/EFCore.Tests/ChangeTracking/CollectionEntryTest.cs index 24f3529da72..f0be0230201 100644 --- a/test/EFCore.Tests/ChangeTracking/CollectionEntryTest.cs +++ b/test/EFCore.Tests/ChangeTracking/CollectionEntryTest.cs @@ -429,11 +429,14 @@ public void IsModified_can_set_fk_to_modified_principal_with_Modified_dependents EntityState principalState, EntityState dependentState) { using var context = new FreezerContext(); - var cherry = new Cherry(); + var cherry = new Cherry { Id = 1 }; var chunky1 = new Chunky { Id = 1, Garcia = cherry }; var chunky2 = new Chunky { Id = 2, Garcia = cherry }; cherry.Monkeys = new List { chunky1, chunky2 }; + context.Attach(chunky1); + context.Attach(chunky2); + context.Entry(cherry).State = principalState; context.Entry(chunky1).State = dependentState; context.Entry(chunky2).State = dependentState; diff --git a/test/EFCore.Tests/ChangeTracking/ReferenceEntryTest.cs b/test/EFCore.Tests/ChangeTracking/ReferenceEntryTest.cs index 9f79555e0c4..ef08e103a04 100644 --- a/test/EFCore.Tests/ChangeTracking/ReferenceEntryTest.cs +++ b/test/EFCore.Tests/ChangeTracking/ReferenceEntryTest.cs @@ -351,6 +351,37 @@ public void IsModified_can_set_fk_to_modified() Assert.Equal(EntityState.Unchanged, entityEntry.State); } + [ConditionalFact] + public void IsModified_can_reject_changes_to_an_fk() + { + using var context = new FreezerContext(); + var cherry = new Cherry(); + var chunky = new Chunky { Garcia = cherry }; + cherry.Monkeys = new List { chunky }; + context.AttachRange(cherry, chunky); + + var entityEntry = context.Entry(chunky); + var reference = entityEntry.Reference(e => e.Garcia); + var originalValue = entityEntry.Property(e => e.GarciaId).CurrentValue; + + Assert.False(reference.IsModified); + + entityEntry.Property(e => e.GarciaId).CurrentValue = 77; + + Assert.True(reference.IsModified); + Assert.True(entityEntry.Property(e => e.GarciaId).IsModified); + Assert.Equal(77, entityEntry.Property(e => e.GarciaId).CurrentValue); + Assert.Equal(originalValue, entityEntry.Property(e => e.GarciaId).OriginalValue); + + reference.IsModified = false; + + Assert.False(reference.IsModified); + Assert.False(entityEntry.Property(e => e.GarciaId).IsModified); + Assert.Equal(originalValue, entityEntry.Property(e => e.GarciaId).CurrentValue); + Assert.Equal(originalValue, entityEntry.Property(e => e.GarciaId).OriginalValue); + Assert.Equal(EntityState.Unchanged, entityEntry.State); + } + [ConditionalFact] public void IsModified_tracks_state_of_FK_property_principal() { @@ -454,10 +485,12 @@ public void IsModified_can_set_fk_to_modified_principal_with_Modified_dependent( EntityState principalState, EntityState dependentState) { using var context = new FreezerContext(); - var half = new Half(); + var half = new Half { Id = 7 }; var chunky = new Chunky { Id = 1, Baked = half }; half.Monkey = chunky; + context.Attach(half); + context.Entry(chunky).State = principalState; context.Entry(half).State = dependentState;