Skip to content

Commit 88a2fb1

Browse files
committed
[6.0] Flag all properties as not unknown when saving changes
Fixes #26330
1 parent 9e3ee59 commit 88a2fb1

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

Diff for: src/EFCore/ChangeTracking/Internal/InternalEntityEntry.cs

+1
Original file line numberDiff line numberDiff line change
@@ -1436,6 +1436,7 @@ public void AcceptChanges()
14361436
}
14371437

14381438
_stateData.FlagAllProperties(EntityType.PropertyCount(), PropertyFlag.IsTemporary, false);
1439+
_stateData.FlagAllProperties(EntityType.PropertyCount(), PropertyFlag.Unknown, false);
14391440

14401441
var currentState = EntityState;
14411442
if ((currentState == EntityState.Unchanged)

Diff for: test/EFCore.Specification.Tests/GraphUpdates/GraphUpdatesTestBase.cs

+33
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
434434
b => b.HasKey("OwnerWithKeyedCollectionId", "PrivateKey"));
435435
});
436436

437+
modelBuilder
438+
.Entity<OwnerWithNonCompositeOwnedCollection>()
439+
.OwnsMany(e => e.Owned, owned => owned.HasKey("Id"));
440+
437441
modelBuilder.Entity<OwnerNoKeyGeneration>(
438442
b =>
439443
{
@@ -3287,6 +3291,35 @@ public string Bar
32873291
}
32883292
}
32893293

3294+
protected class OwnerWithNonCompositeOwnedCollection : NotifyingEntity
3295+
{
3296+
private int _id;
3297+
private ICollection<NonCompositeOwnedCollection> _owned = new ObservableHashSet<NonCompositeOwnedCollection>();
3298+
3299+
public int Id
3300+
{
3301+
get => _id;
3302+
set => SetWithNotify(value, ref _id);
3303+
}
3304+
3305+
public ICollection<NonCompositeOwnedCollection> Owned
3306+
{
3307+
get => _owned;
3308+
set => SetWithNotify(value, ref _owned);
3309+
}
3310+
}
3311+
3312+
protected class NonCompositeOwnedCollection : NotifyingEntity
3313+
{
3314+
private string _foo;
3315+
3316+
public string Foo
3317+
{
3318+
get => _foo;
3319+
set => SetWithNotify(value, ref _foo);
3320+
}
3321+
}
3322+
32903323
protected class OwnerNoKeyGeneration : NotifyingEntity
32913324
{
32923325
private int _id;

Diff for: test/EFCore.Specification.Tests/GraphUpdates/GraphUpdatesTestBaseMiscellaneous.cs

+53
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,59 @@ await ExecuteWithStrategyInTransactionAsync(
296296
});
297297
}
298298

299+
[ConditionalTheory] // Issue #26330
300+
[InlineData(false)]
301+
[InlineData(true)]
302+
public virtual async Task Saving_unknown_key_value_marks_it_as_unmodified(bool async)
303+
{
304+
await ExecuteWithStrategyInTransactionAsync(
305+
async context =>
306+
{
307+
var owner = new OwnerWithNonCompositeOwnedCollection();
308+
owner.Owned.Add(new() { Foo = "Milan" });
309+
310+
if (async)
311+
{
312+
await context.AddAsync(owner);
313+
await context.SaveChangesAsync();
314+
}
315+
else
316+
{
317+
context.Add(owner);
318+
context.SaveChanges();
319+
}
320+
321+
owner.Owned.Remove(owner.Owned.Single());
322+
owner.Owned.Add(new() { Foo = "Rome" });
323+
324+
if (Fixture.ForceClientNoAction)
325+
{
326+
await Assert.ThrowsAsync<InvalidOperationException>(
327+
async () =>
328+
_ = async
329+
? await context.SaveChangesAsync()
330+
: context.SaveChanges());
331+
}
332+
else
333+
{
334+
_ = async
335+
? await context.SaveChangesAsync()
336+
: context.SaveChanges();
337+
}
338+
},
339+
async context =>
340+
{
341+
if (!Fixture.ForceClientNoAction)
342+
{
343+
var owner = async
344+
? await context.Set<OwnerWithNonCompositeOwnedCollection>().SingleAsync()
345+
: context.Set<OwnerWithNonCompositeOwnedCollection>().Single();
346+
347+
Assert.Equal("Rome", owner.Owned.Single().Foo);
348+
}
349+
});
350+
}
351+
299352
[ConditionalTheory] // Issue #19856
300353
[InlineData(false)]
301354
[InlineData(true)]

Diff for: test/EFCore.SqlServer.FunctionalTests/GraphUpdates/GraphUpdatesSqlServerOwnedTest.cs

+4
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,10 @@ protected override void OnModelCreating(ModelBuilder modelBuilder, DbContext con
454454
b => b.HasKey("OwnerWithKeyedCollectionId", "PrivateKey"));
455455
});
456456

457+
modelBuilder
458+
.Entity<OwnerWithNonCompositeOwnedCollection>()
459+
.OwnsMany(e => e.Owned, owned => owned.HasKey("Id"));
460+
457461
modelBuilder.Entity<OwnerNoKeyGeneration>(
458462
b =>
459463
{

0 commit comments

Comments
 (0)