Skip to content

Commit

Permalink
[6.0] Flag all properties as not unknown when saving changes
Browse files Browse the repository at this point in the history
Fixes #26330
  • Loading branch information
ajcvickers committed Oct 14, 2021
1 parent 9e3ee59 commit b08bfb0
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/EFCore/ChangeTracking/Internal/InternalEntityEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,7 @@ public void AcceptChanges()
}

_stateData.FlagAllProperties(EntityType.PropertyCount(), PropertyFlag.IsTemporary, false);
_stateData.FlagAllProperties(EntityType.PropertyCount(), PropertyFlag.Unknown, false);

var currentState = EntityState;
if ((currentState == EntityState.Unchanged)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
b => b.HasKey("OwnerWithKeyedCollectionId", "PrivateKey"));
});

modelBuilder
.Entity<OwnerWithNonCompositeOwnedCollection>()
.OwnsMany(e => e.Owned, owned => owned.HasKey("Id"));

modelBuilder.Entity<OwnerNoKeyGeneration>(
b =>
{
Expand Down Expand Up @@ -3287,6 +3291,35 @@ public string Bar
}
}

protected class OwnerWithNonCompositeOwnedCollection : NotifyingEntity
{
private int _id;
private ICollection<NonCompositeOwnedCollection> _owned = new ObservableHashSet<NonCompositeOwnedCollection>();

public int Id
{
get => _id;
set => SetWithNotify(value, ref _id);
}

public ICollection<NonCompositeOwnedCollection> Owned
{
get => _owned;
set => SetWithNotify(value, ref _owned);
}
}

protected class NonCompositeOwnedCollection : NotifyingEntity
{
private string _foo;

public string Foo
{
get => _foo;
set => SetWithNotify(value, ref _foo);
}
}

protected class OwnerNoKeyGeneration : NotifyingEntity
{
private int _id;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,59 @@ await ExecuteWithStrategyInTransactionAsync(
});
}

[ConditionalTheory] // Issue #26330
[InlineData(false)]
[InlineData(true)]
public virtual async Task Saving_unknown_key_value_marks_it_as_unmodified(bool async)
{
await ExecuteWithStrategyInTransactionAsync(
async context =>
{
var owner = new OwnerWithNonCompositeOwnedCollection();
owner.Owned.Add(new() { Foo = "Milan" });

if (async)
{
await context.AddAsync(owner);
await context.SaveChangesAsync();
}
else
{
context.Add(owner);
context.SaveChanges();
}

owner.Owned.Remove(owner.Owned.Single());
owner.Owned.Add(new() { Foo = "Rome" });

if (Fixture.ForceClientNoAction)
{
await Assert.ThrowsAsync<InvalidOperationException>(
async () =>
_ = async
? await context.SaveChangesAsync()
: context.SaveChanges());
}
else
{
_ = async
? await context.SaveChangesAsync()
: context.SaveChanges();
}
},
async context =>
{
if (!Fixture.ForceClientNoAction)
{
var owner = async
? await context.Set<OwnerWithNonCompositeOwnedCollection>().SingleAsync()
: context.Set<OwnerWithNonCompositeOwnedCollection>().Single();
Assert.Equal("Rome", owner.Owned.Single().Foo);
}
});
}
[ConditionalTheory] // Issue #19856
[InlineData(false)]
[InlineData(true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
b => b.HasKey("OwnerWithKeyedCollectionId", "PrivateKey"));
});

modelBuilder
.Entity<OwnerWithNonCompositeOwnedCollection>()
.OwnsMany(e => e.Owned, owned => owned.HasKey("Id"));

modelBuilder.Entity<OwnerNoKeyGeneration>(
b =>
{
Expand Down

0 comments on commit b08bfb0

Please sign in to comment.