Skip to content
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
17 changes: 11 additions & 6 deletions src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2215,17 +2215,22 @@ private static string GetFullName(IReadOnlyEntityType entityType)
return entityTypeName;
}

if (entityType.HasSharedClrType
&& entityTypeName
== ownership.PrincipalEntityType.GetOwnedName(
entityType.ClrType.ShortDisplayName(), ownership.PrincipalToDependent!.Name))
var ownerNavigation = ownership.PrincipalToDependent!.Name;
if (entityType.HasSharedClrType)
{
entityTypeName = entityType.ClrType.DisplayName();
if (entityTypeName == ownership.PrincipalEntityType.GetOwnedName(entityType.ClrType.ShortDisplayName(), ownerNavigation))
{
entityTypeName = entityType.ClrType.DisplayName();
}
else if (entityTypeName == ownership.PrincipalEntityType.GetOwnedName(entityType.ShortName(), ownerNavigation))
{
entityTypeName = entityType.ShortName();
}
}

return GetFullName(ownership.PrincipalEntityType)
+ "."
+ ownership.PrincipalToDependent!.Name
+ ownerNavigation
+ "#"
+ entityTypeName;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,61 @@ protected override void BuildModel(ModelBuilder modelBuilder)
Assert.Single(snapshotModel.FindEntityType("Dependent")!.GetForeignKeys());
}

[Fact] // Issue #36180
public void Owned_collection_with_one_to_one_reference_round_trips()
{
// Regenerating the snapshot from a model that was itself built from a snapshot (as happens
// when removing a migration) must not corrupt the owned entity type name used in the
// HasForeignKey call of a one-to-one relationship declared inside an owned collection.
var modelBuilder = CreateConventionalModelBuilder();
modelBuilder.HasDefaultSchema(null);
modelBuilder.Model.RemoveAnnotation(CoreAnnotationNames.ProductVersion);
modelBuilder.Entity<Cart>(b =>
{
b.HasKey(e => e.Id);
b.OwnsMany(
e => e.CartProducts,
ownedBuilder =>
{
ownedBuilder.WithOwner().HasForeignKey(nameof(CartProduct.CartId));
ownedBuilder.HasKey(nameof(CartProduct.CartId), nameof(CartProduct.ProductId));
ownedBuilder.HasOne(e => e.Product).WithOne().HasForeignKey<CartProduct>(e => e.ProductId);
});
});
modelBuilder.Entity<Product>();

var model = modelBuilder.FinalizeModel(designTime: true, skipValidation: true);

var generator = CreateMigrationsGenerator();
var code = generator.GenerateSnapshot("RootNamespace", typeof(DbContext), "Snapshot", model);

var roundTrippedModel = BuildModelFromSnapshotSource(code);
var roundTrippedCode = generator.GenerateSnapshot("RootNamespace", typeof(DbContext), "Snapshot", roundTrippedModel);

Assert.Equal(code, roundTrippedCode, ignoreLineEndingDifferences: true);

// The regenerated snapshot must still compile into a valid model.
Assert.NotNull(BuildModelFromSnapshotSource(roundTrippedCode));
}

private class Cart
{
public int Id { get; set; }
public List<CartProduct> CartProducts { get; set; } = [];
}

private class CartProduct
{
public int CartId { get; set; }
public int ProductId { get; set; }
public Product Product { get; set; } = null!;
}

private class Product
{
public int Id { get; set; }
}

[Fact]
public void Snapshot_with_migration_id()
{
Expand Down
Loading