From 0ec4669e6ae5549b83e8b8a4d5ea1c86c6effef7 Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Mon, 15 Aug 2022 18:16:15 -0700 Subject: [PATCH] Generate identity SQL for different seeds in TPC Fixes #28195 --- .../Extensions/SqlServerPropertyExtensions.cs | 2 +- .../Internal/SqlServerAnnotationProvider.cs | 3 +- .../Migrations/MigrationsSqlServerTest.cs | 53 +++++++++++++++++++ 3 files changed, 55 insertions(+), 3 deletions(-) diff --git a/src/EFCore.SqlServer/Extensions/SqlServerPropertyExtensions.cs b/src/EFCore.SqlServer/Extensions/SqlServerPropertyExtensions.cs index f56c0712e6b..536c33af8c7 100644 --- a/src/EFCore.SqlServer/Extensions/SqlServerPropertyExtensions.cs +++ b/src/EFCore.SqlServer/Extensions/SqlServerPropertyExtensions.cs @@ -741,7 +741,7 @@ internal static SqlServerValueGenerationStrategy GetValueGenerationStrategy( { return (SqlServerValueGenerationStrategy?)@override.Value ?? SqlServerValueGenerationStrategy.None; } - + var annotation = property.FindAnnotation(SqlServerAnnotationNames.ValueGenerationStrategy); if (annotation?.Value != null && StoreObjectIdentifier.Create(property.DeclaringEntityType, storeObject.StoreObjectType) == storeObject) diff --git a/src/EFCore.SqlServer/Metadata/Internal/SqlServerAnnotationProvider.cs b/src/EFCore.SqlServer/Metadata/Internal/SqlServerAnnotationProvider.cs index fabd24a58be..80b2364a49a 100644 --- a/src/EFCore.SqlServer/Metadata/Internal/SqlServerAnnotationProvider.cs +++ b/src/EFCore.SqlServer/Metadata/Internal/SqlServerAnnotationProvider.cs @@ -219,8 +219,7 @@ public override IEnumerable For(IColumn column, bool designTime) } var table = StoreObjectIdentifier.Table(column.Table.Name, column.Table.Schema); - var identityProperty = column.PropertyMappings.Where( - m => m.TableMapping.EntityType == m.Property.DeclaringEntityType) + var identityProperty = column.PropertyMappings .Select(m => m.Property) .FirstOrDefault( p => p.GetValueGenerationStrategy(table) diff --git a/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs b/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs index 5764028c12e..b7b5a164371 100644 --- a/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs +++ b/test/EFCore.SqlServer.FunctionalTests/Migrations/MigrationsSqlServerTest.cs @@ -783,6 +783,59 @@ await Test( @"ALTER TABLE [People] ADD [IdentityColumn] int NOT NULL IDENTITY(100, 5);"); } + [ConditionalFact] + public virtual async Task Add_column_identity_seed_increment_for_TPC() + { + await Test( + builder => + { + builder.Entity("Animal").UseTpcMappingStrategy().Property("Id"); + builder.Entity("Cat").HasBaseType("Animal").ToTable("Cats"); + builder.Entity("Dog").HasBaseType("Animal").ToTable("Dogs"); + }, + builder => { }, + builder => + { + builder.Entity("Animal") + .Property("IdentityColumn"); + builder.Entity("Cat").ToTable("Cats", tb => tb.Property("IdentityColumn").UseIdentityColumn(1, 2)); + builder.Entity("Dog").ToTable("Dogs", tb => tb.Property("IdentityColumn").UseIdentityColumn(2, 2)); + }, + model => + { + Assert.Collection(model.Tables, + t => + { + Assert.Equal("Animal", t.Name); + var column = Assert.Single(t.Columns, c => c.Name == "IdentityColumn"); + Assert.Null(column.ValueGenerated); + }, + t => + { + Assert.Equal("Cats", t.Name); + var column = Assert.Single(t.Columns, c => c.Name == "IdentityColumn"); + Assert.Equal(ValueGenerated.OnAdd, column.ValueGenerated); + // TODO: Do we not reverse-engineer identity facets? + // Assert.Equal(100, column[SqlServerAnnotationNames.IdentitySeed]); + // Assert.Equal(5, column[SqlServerAnnotationNames.IdentityIncrement]); + }, + t => + { + Assert.Equal("Dogs", t.Name); + var column = Assert.Single(t.Columns, c => c.Name == "IdentityColumn"); + Assert.Equal(ValueGenerated.OnAdd, column.ValueGenerated); + // TODO: Do we not reverse-engineer identity facets? + // Assert.Equal(100, column[SqlServerAnnotationNames.IdentitySeed]); + // Assert.Equal(5, column[SqlServerAnnotationNames.IdentityIncrement]); + }); + }); + + AssertSql( + @"ALTER TABLE [Dogs] ADD [IdentityColumn] int NOT NULL IDENTITY(2, 2);", + "ALTER TABLE [Cats] ADD [IdentityColumn] int NOT NULL IDENTITY(1, 2);", + "ALTER TABLE [Animal] ADD [IdentityColumn] int NOT NULL DEFAULT 0;"); + } + public override async Task Alter_column_change_type() { await base.Alter_column_change_type();