Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include table name for overrides in snapshot. #29819

Merged
merged 1 commit into from
Dec 12, 2022
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 @@ -845,9 +845,6 @@ private void GenerateTableMapping(
annotations.TryGetAndRemove(RelationalAnnotationNames.TableName, out IAnnotation tableNameAnnotation);
var table = StoreObjectIdentifier.Create(entityType, StoreObjectType.Table);
var tableName = (string?)tableNameAnnotation?.Value ?? table?.Name;
var explicitName = tableNameAnnotation != null
|| entityType.BaseType == null
|| entityType.BaseType.GetTableName() != tableName;

annotations.TryGetAndRemove(RelationalAnnotationNames.Schema, out IAnnotation schemaAnnotation);
var schema = (string?)schemaAnnotation?.Value ?? table?.Schema;
Expand All @@ -861,6 +858,12 @@ private void GenerateTableMapping(
var hasTriggers = entityType.GetDeclaredTriggers().Any(t => t.GetTableName() == tableName! && t.GetTableSchema() == schema);
var hasOverrides = table != null
&& entityType.GetProperties().Select(p => p.FindOverrides(table.Value)).Any(o => o != null);

var explicitName = tableNameAnnotation != null
|| entityType.BaseType == null
|| entityType.BaseType.GetTableName() != tableName
|| hasOverrides;

var requiresTableBuilder = isExcludedFromMigrations
|| comment != null
|| hasTriggers
Expand Down
81 changes: 81 additions & 0 deletions test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,11 @@ private class DerivedEntity : BaseEntity
public string Name { get; set; }
}

private class DuplicateDerivedEntity : BaseEntity
{
public string Name { get; set; }
}

private class AnotherDerivedEntity : BaseEntity
{
public string Title { get; set; }
Expand Down Expand Up @@ -4212,6 +4217,82 @@ public virtual void Property_column_name_annotation_is_stored_in_snapshot_as_flu
});"),
o => Assert.Equal("CName", o.GetEntityTypes().First().FindProperty("AlternateId")["Relational:ColumnName"]));

[ConditionalFact]
public virtual void Property_column_name_on_specific_table_is_stored_in_snapshot_as_fluent_api()
=> Test(
builder =>
{
builder.Entity<DerivedEntity>().HasBaseType<BaseEntity>();
builder.Entity<DuplicateDerivedEntity>().HasBaseType<BaseEntity>();
},
AddBoilerPlate(
GetHeading()
+ @"
modelBuilder.Entity(""Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+BaseEntity"", b =>
{
b.Property<int>(""Id"")
.ValueGeneratedOnAdd()
.HasColumnType(""int"");

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

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

b.HasKey(""Id"");

b.ToTable(""BaseEntity"");

b.HasDiscriminator<string>(""Discriminator"").HasValue(""BaseEntity"");

b.UseTphMappingStrategy();
});

modelBuilder.Entity(""Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+DerivedEntity"", b =>
{
b.HasBaseType(""Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+BaseEntity"");

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

b.HasDiscriminator().HasValue(""DerivedEntity"");
});

modelBuilder.Entity(""Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+DuplicateDerivedEntity"", b =>
{
b.HasBaseType(""Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+BaseEntity"");

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

b.ToTable(""BaseEntity"", t =>
{
t.Property(""Name"")
.HasColumnName(""DuplicateDerivedEntity_Name"");
});

b.HasDiscriminator().HasValue(""DuplicateDerivedEntity"");
});"),
o =>
{
Assert.Equal(3, o.GetEntityTypes().Count());
Assert.Collection(
o.GetEntityTypes(),
t => Assert.Equal("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+BaseEntity", t.Name),
t => Assert.Equal("Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+DerivedEntity", t.Name),
t =>
{
Assert.Equal(
"Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+DuplicateDerivedEntity", t.Name);
Assert.Equal(
"DuplicateDerivedEntity_Name",
t.FindProperty(nameof(DuplicateDerivedEntity.Name))
.GetColumnName(StoreObjectIdentifier.Table(nameof(BaseEntity))));
}
);
});

[ConditionalFact]
public virtual void Property_column_type_annotation_is_stored_in_snapshot_as_fluent_api()
=> Test(
Expand Down