Skip to content

Commit

Permalink
Generate identity SQL for different seeds in TPC (#28742)
Browse files Browse the repository at this point in the history
  • Loading branch information
AndriySvyryd authored Aug 16, 2022
1 parent 0577a3a commit 4dd5074
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,7 @@ public override IEnumerable<IAnnotation> 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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>("Id");
builder.Entity("Cat").HasBaseType("Animal").ToTable("Cats");
builder.Entity("Dog").HasBaseType("Animal").ToTable("Dogs");
},
builder => { },
builder =>
{
builder.Entity("Animal")
.Property<int>("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();
Expand Down

0 comments on commit 4dd5074

Please sign in to comment.