diff --git a/src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs b/src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs index 559b295a1e6..75edcecadea 100644 --- a/src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs +++ b/src/EFCore.Design/Migrations/Design/CSharpSnapshotGenerator.cs @@ -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; @@ -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 diff --git a/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs b/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs index f53c8d08d79..cff276f1e85 100644 --- a/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs +++ b/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs @@ -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; } @@ -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().HasBaseType(); + builder.Entity().HasBaseType(); + }, + AddBoilerPlate( + GetHeading() + + @" + modelBuilder.Entity(""Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+BaseEntity"", b => + { + b.Property(""Id"") + .ValueGeneratedOnAdd() + .HasColumnType(""int""); + + SqlServerPropertyBuilderExtensions.UseIdentityColumn(b.Property(""Id"")); + + b.Property(""Discriminator"") + .IsRequired() + .HasColumnType(""nvarchar(max)""); + + b.HasKey(""Id""); + + b.ToTable(""BaseEntity""); + + b.HasDiscriminator(""Discriminator"").HasValue(""BaseEntity""); + + b.UseTphMappingStrategy(); + }); + + modelBuilder.Entity(""Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+DerivedEntity"", b => + { + b.HasBaseType(""Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+BaseEntity""); + + b.Property(""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(""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(