Skip to content

Commit

Permalink
Restore original value when setting IsModified false on navigation (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ajcvickers committed Jun 15, 2020
1 parent 283dc27 commit 83ef429
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/EFCore/ChangeTracking/NavigationEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
5 changes: 4 additions & 1 deletion test/EFCore.Tests/ChangeTracking/CollectionEntryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Chunky> { chunky1, chunky2 };

context.Attach(chunky1);
context.Attach(chunky2);

context.Entry(cherry).State = principalState;
context.Entry(chunky1).State = dependentState;
context.Entry(chunky2).State = dependentState;
Expand Down
35 changes: 34 additions & 1 deletion test/EFCore.Tests/ChangeTracking/ReferenceEntryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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> { 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()
{
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit 83ef429

Please sign in to comment.