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
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,9 @@ public static string GetDefaultColumnName(this IReadOnlyProperty property)
var foreignKey = property.GetContainingForeignKeys().First();
var principalEntityType = foreignKey.PrincipalEntityType;
if (principalEntityType is { HasSharedClrType: false, ClrType.IsConstructedGenericType: true }
&& foreignKey.DependentToPrincipal == null)
&& foreignKey.DependentToPrincipal == null
&& (principalEntityType.GetTableName() != foreignKey.DeclaringEntityType.GetTableName()
|| principalEntityType.GetSchema() != foreignKey.DeclaringEntityType.GetSchema()))
{
var principalProperty = property.FindFirstPrincipal()!;
var principalName = principalEntityType.ShortName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,25 @@ public class FooExtension<T>
public T Bar { get; set; }
}

public class Parrot<TChild>
{
public int Id { get; set; }
public string Name { get; set; }
public TChild Child { get; set; }
}

public class Parrot
{
public int Id { get; set; }
public string Name { get; set; }
public Beak Child { get; set; }
}

public class Beak
{
public string Name { get; set; }
}

#region Model

[ConditionalFact]
Expand Down Expand Up @@ -4885,6 +4904,139 @@ public virtual void Property_column_name_is_stored_in_snapshot_when_DefaultColum
var property = entityType.FindProperty("FooExtensionId");
Assert.NotNull(property);
Assert.Equal("FooExtension<BarA>Id", property.GetColumnName());

Assert.Collection(
model.GetRelationalModel().Tables,
t =>
{

Assert.Equal("BarBase", t.Name);
Assert.Equal(["Id", "Discriminator", "FooExtension<BarA>Id"], t.Columns.Select(t => t.Name));
},
t =>
{

Assert.Equal("FooExtension<BarA>", t.Name);
Assert.Equal(["Id"], t.Columns.Select(t => t.Name));
});
});

[ConditionalFact]
public virtual void Generic_entity_type_with_owned_entities()
=> Test(
modelBuilder => modelBuilder.Entity<Parrot<Beak>>().OwnsOne(e => e.Child),
AddBoilerPlate(
"""
modelBuilder
.HasDefaultSchema("DefaultSchema")
.HasAnnotation("Relational:MaxIdentifierLength", 128);

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGeneratorTest+Parrot<Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGeneratorTest+Beak>", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));

b.Property<string>("Name")
.HasColumnType("nvarchar(max)");

b.HasKey("Id");

b.ToTable("Parrot<Beak>", "DefaultSchema");
});

modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGeneratorTest+Parrot<Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGeneratorTest+Beak>", b =>
{
b.OwnsOne("Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGeneratorTest+Beak", "Child", b1 =>
{
b1.Property<int>("ParrotId")
.HasColumnType("int");

b1.Property<string>("Name")
.HasColumnType("nvarchar(max)");

b1.HasKey("ParrotId");

b1.ToTable("Parrot<Beak>", "DefaultSchema");

b1.WithOwner()
.HasForeignKey("ParrotId");
});

b.Navigation("Child");
});
"""),
model =>
{
var parentType = model.FindEntityType("Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGeneratorTest+Parrot<Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGeneratorTest+Beak>");
Assert.NotNull(parentType);
Assert.NotNull(parentType.FindNavigation("Child")!.TargetEntityType);

var table = model.GetRelationalModel().Tables.Single();
Assert.Equal(["Id", "Child_Name", "Name"], table.Columns.Select(t => t.Name));
});

[ConditionalFact]
public virtual void Non_generic_entity_type_with_owned_entities()
=> Test(
modelBuilder => modelBuilder.Entity<Parrot>().OwnsOne(e => e.Child),
AddBoilerPlate(
"""
modelBuilder
.HasDefaultSchema("DefaultSchema")
.HasAnnotation("Relational:MaxIdentifierLength", 128);

SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);

modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGeneratorTest+Parrot", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("int");

SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property<int>("Id"));

b.Property<string>("Name")
.HasColumnType("nvarchar(max)");

b.HasKey("Id");

b.ToTable("Parrot", "DefaultSchema");
});

modelBuilder.Entity("Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGeneratorTest+Parrot", b =>
{
b.OwnsOne("Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGeneratorTest+Beak", "Child", b1 =>
{
b1.Property<int>("ParrotId")
.HasColumnType("int");

b1.Property<string>("Name")
.HasColumnType("nvarchar(max)");

b1.HasKey("ParrotId");

b1.ToTable("Parrot", "DefaultSchema");

b1.WithOwner()
.HasForeignKey("ParrotId");
});

b.Navigation("Child");
});
"""),
model =>
{
var parentType = model.FindEntityType("Microsoft.EntityFrameworkCore.Migrations.Design.CSharpMigrationsGeneratorTest+Parrot");
Assert.NotNull(parentType);
Assert.NotNull(parentType.FindNavigation("Child")!.TargetEntityType);

var table = model.GetRelationalModel().Tables.Single();
Assert.Equal(["Id", "Child_Name", "Name"], table.Columns.Select(t => t.Name));
});

[ConditionalFact]
Expand Down