Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore original value when setting IsModified false on navigation #21260

Merged
merged 1 commit into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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