diff --git a/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs b/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs index 469cac98349..21ff4789482 100644 --- a/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs +++ b/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs @@ -1898,6 +1898,7 @@ protected virtual Dictionary> DiffData( principalSourceTable = mainSourceEntityType.GetTableMappings().First().Table; } + var useOldBehavior = AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue23792", out var enabled) && enabled; foreach (var sourceSeed in sourceEntityType.GetSeedData()) { var sourceEntry = GetEntry(sourceSeed, sourceEntityType, _sourceUpdateAdapter); @@ -1984,7 +1985,8 @@ protected virtual Dictionary> DiffData( if (sourceProperty == null) { if (targetProperty.GetAfterSaveBehavior() != PropertySaveBehavior.Save - && (targetProperty.ValueGenerated & ValueGenerated.OnUpdate) == 0) + && (targetProperty.ValueGenerated & ValueGenerated.OnUpdate) == 0 + && (useOldBehavior || targetKeyMap.Count == 1 || entry.EntityType.Name == sourceEntityType.Name)) { entryMapping.RecreateRow = true; break; diff --git a/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs b/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs index a42866f48cf..48f86607d9b 100644 --- a/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs +++ b/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs @@ -8217,6 +8217,98 @@ public void Update_AK_seed_value_with_a_referencing_foreign_key() })); } + [ConditionalFact] + public void SeedData_with_guid_AK_and_multiple_owned_types() + { + Execute( + target => + { + target.Entity( + builder => + { + builder.HasAlternateKey(x => x.Guid); + builder.Property(x => x.Id).ValueGeneratedNever(); + + var data = new[] + { + new SomeEntity(1L, new Guid("74520CF7-0C78-447C-8FE0-ED97A16A13F5")) + }; + + var owned = data.Select(x => new + { + SomeEntityId = x.Id, + }).ToArray(); + + builder.OwnsOne(x => x.OwnedEntity).HasData(owned); + builder.HasData(data); + }); + + target.Entity( + builder => { + builder.HasAlternateKey(x => x.Guid); + + var data = new[] + { + new ApplicationUser() + { + Id = 12345, + Guid = new Guid("4C85B629-732A-4724-AA33-6E8108134BAE") + } + }; + + var owned = data.Select(x => new + { + ApplicationUserId = x.Id, + }).ToArray(); + + builder.OwnsOne(x => x.OwnedEntity).HasData(owned); + builder.HasData(data); + }); + }, + target => { }, + source => { }, + Assert.Empty, + Assert.Empty); + } + + protected class SomeEntity + { + public SomeEntity(long id, Guid guid) + { + Id = id; + Guid = guid; + } + + public virtual SomeOwnedEntity OwnedEntity { get; private set; } = new SomeOwnedEntity(); + + public Guid Guid { get; protected set; } + + public long Id { get; protected set; } + + } + + protected class ApplicationUser + { + private readonly SomeOwnedEntity _ownedEntity; + + + public ApplicationUser() + { + _ownedEntity = null!; + } + + public virtual long Id { get; set; } + + public virtual SomeOwnedEntity OwnedEntity => _ownedEntity; + + public Guid Guid { get; set; } + + } + + protected class SomeOwnedEntity + { + } + [ConditionalFact] public void SeedData_and_PK_rename() {