Skip to content

Commit

Permalink
Include table name for overrides in snapshot.
Browse files Browse the repository at this point in the history
Fixes #29534
  • Loading branch information
AndriySvyryd committed Dec 9, 2022
1 parent bf20e32 commit 0249474
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 3 deletions.
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(DuplicateDerivedEntity.Name))));
}
);
});

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

0 comments on commit 0249474

Please sign in to comment.