Skip to content

Commit 84506bb

Browse files
committed
Correctly distinguish between seeds for different owned types
Fixes #23792
1 parent cc8cc5c commit 84506bb

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1984,7 +1984,8 @@ protected virtual Dictionary<IEntityType, List<ITable>> DiffData(
19841984
if (sourceProperty == null)
19851985
{
19861986
if (targetProperty.GetAfterSaveBehavior() != PropertySaveBehavior.Save
1987-
&& (targetProperty.ValueGenerated & ValueGenerated.OnUpdate) == 0)
1987+
&& (targetProperty.ValueGenerated & ValueGenerated.OnUpdate) == 0
1988+
&& (targetKeyMap.Count == 1 || entry.EntityType.Name == sourceEntityType.Name))
19881989
{
19891990
entryMapping.RecreateRow = true;
19901991
break;

test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8217,6 +8217,98 @@ public void Update_AK_seed_value_with_a_referencing_foreign_key()
82178217
}));
82188218
}
82198219

8220+
[ConditionalFact]
8221+
public void SeedData_with_guid_AK_and_multiple_owned_types()
8222+
{
8223+
Execute(
8224+
target =>
8225+
{
8226+
target.Entity<SomeEntity>(
8227+
builder =>
8228+
{
8229+
builder.HasAlternateKey(x => x.Guid);
8230+
builder.Property(x => x.Id).ValueGeneratedNever();
8231+
8232+
var data = new[]
8233+
{
8234+
new SomeEntity(1L, new Guid("74520CF7-0C78-447C-8FE0-ED97A16A13F5"))
8235+
};
8236+
8237+
var owned = data.Select(x => new
8238+
{
8239+
SomeEntityId = x.Id,
8240+
}).ToArray();
8241+
8242+
builder.OwnsOne(x => x.OwnedEntity).HasData(owned);
8243+
builder.HasData(data);
8244+
});
8245+
8246+
target.Entity<ApplicationUser>(
8247+
builder => {
8248+
builder.HasAlternateKey(x => x.Guid);
8249+
8250+
var data = new[]
8251+
{
8252+
new ApplicationUser()
8253+
{
8254+
Id = 12345,
8255+
Guid = new Guid("4C85B629-732A-4724-AA33-6E8108134BAE")
8256+
}
8257+
};
8258+
8259+
var owned = data.Select(x => new
8260+
{
8261+
ApplicationUserId = x.Id,
8262+
}).ToArray();
8263+
8264+
builder.OwnsOne(x => x.OwnedEntity).HasData(owned);
8265+
builder.HasData(data);
8266+
});
8267+
},
8268+
target => { },
8269+
source => { },
8270+
Assert.Empty,
8271+
Assert.Empty);
8272+
}
8273+
8274+
protected class SomeEntity
8275+
{
8276+
public SomeEntity(long id, Guid guid)
8277+
{
8278+
Id = id;
8279+
Guid = guid;
8280+
}
8281+
8282+
public virtual SomeOwnedEntity OwnedEntity { get; private set; } = new SomeOwnedEntity();
8283+
8284+
public Guid Guid { get; protected set; }
8285+
8286+
public long Id { get; protected set; }
8287+
8288+
}
8289+
8290+
protected class ApplicationUser
8291+
{
8292+
private readonly SomeOwnedEntity _ownedEntity;
8293+
8294+
8295+
public ApplicationUser()
8296+
{
8297+
_ownedEntity = null!;
8298+
}
8299+
8300+
public virtual long Id { get; set; }
8301+
8302+
public virtual SomeOwnedEntity OwnedEntity => _ownedEntity;
8303+
8304+
public Guid Guid { get; set; }
8305+
8306+
}
8307+
8308+
protected class SomeOwnedEntity
8309+
{
8310+
}
8311+
82208312
[ConditionalFact]
82218313
public void SeedData_and_PK_rename()
82228314
{

0 commit comments

Comments
 (0)