Skip to content

Commit

Permalink
Fix snapshot generation with HiLo
Browse files Browse the repository at this point in the history
Fixes #26134
  • Loading branch information
roji committed Sep 22, 2021
1 parent 57b8529 commit 60ec60b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ private static readonly MethodInfo _propertyUseIdentityColumnsMethodInfo
= typeof(SqlServerPropertyBuilderExtensions).GetRequiredRuntimeMethod(
nameof(SqlServerPropertyBuilderExtensions.UseIdentityColumn), typeof(PropertyBuilder), typeof(long), typeof(int));

private static readonly MethodInfo _propertyUseHiLoMethodInfo
= typeof(SqlServerPropertyBuilderExtensions).GetRequiredRuntimeMethod(
nameof(SqlServerPropertyBuilderExtensions.UseHiLo), typeof(PropertyBuilder), typeof(string), typeof(string));

private static readonly MethodInfo _indexIsClusteredMethodInfo
= typeof(SqlServerIndexBuilderExtensions).GetRequiredRuntimeMethod(
nameof(SqlServerIndexBuilderExtensions.IsClustered), typeof(IndexBuilder), typeof(bool));
Expand Down Expand Up @@ -374,7 +378,7 @@ protected override bool IsHandledByConvention(IModel model, IAnnotation annotati
var name = GetAndRemove<string>(annotations, SqlServerAnnotationNames.HiLoSequenceName);
var schema = GetAndRemove<string>(annotations, SqlServerAnnotationNames.HiLoSequenceSchema);
return new(
_modelUseHiLoMethodInfo,
onModel ? _modelUseHiLoMethodInfo : _propertyUseHiLoMethodInfo,
(name, schema) switch
{
(null, null) => Array.Empty<object>(),
Expand Down
40 changes: 40 additions & 0 deletions test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,46 @@ public virtual void Model_annotations_are_stored_in_snapshot()
});
}

[ConditionalFact]
public virtual void Model_Fluent_APIs_are_properly_generated()
{
Test(
builder =>
{
builder.UseHiLo();
builder.Entity<EntityWithOneProperty>();
builder.Ignore<EntityWithTwoProperties>();
},
AddBoilerPlate(
@"
modelBuilder.HasAnnotation(""Relational:MaxIdentifierLength"", 128);
SqlServerModelBuilderExtensions.UseHiLo(modelBuilder, ""EntityFrameworkHiLoSequence"");
modelBuilder.HasSequence(""EntityFrameworkHiLoSequence"")
.IncrementsBy(10);
modelBuilder.Entity(""Microsoft.EntityFrameworkCore.Migrations.ModelSnapshotSqlServerTest+EntityWithOneProperty"", b =>
{
b.Property<int>(""Id"")
.ValueGeneratedOnAdd()
.HasColumnType(""int"");
SqlServerPropertyBuilderExtensions.UseHiLo(b.Property<int>(""Id""));
b.HasKey(""Id"");
b.ToTable(""EntityWithOneProperty"");
});"),
o =>
{
Assert.Equal(SqlServerValueGenerationStrategy.SequenceHiLo, o.GetValueGenerationStrategy());
Assert.Equal(
SqlServerValueGenerationStrategy.SequenceHiLo,
o.GetEntityTypes().Single().GetProperty("Id").GetValueGenerationStrategy());
});
}

[ConditionalFact]
public virtual void Model_default_schema_annotation_is_stored_in_snapshot_as_fluent_api()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ public void GenerateFluentApi_IModel_works_with_identity()
var result = generator.GenerateFluentApiCalls((IModel)modelBuilder.Model, annotations).Single();

Assert.Equal("UseIdentityColumns", result.Method);
Assert.Equal("SqlServerModelBuilderExtensions", result.DeclaringType);

Assert.Collection(
result.Arguments,
Expand All @@ -188,6 +189,7 @@ public void GenerateFluentApi_IProperty_works_with_identity()
var result = generator.GenerateFluentApiCalls((IProperty)property, annotations).Single();

Assert.Equal("UseIdentityColumn", result.Method);
Assert.Equal("SqlServerPropertyBuilderExtensions", result.DeclaringType);

Assert.Collection(
result.Arguments,
Expand All @@ -207,6 +209,7 @@ public void GenerateFluentApi_IProperty_works_with_identity_default_seed_increme
var result = generator.GenerateFluentApiCalls((IProperty)property, annotations).Single();

Assert.Equal("UseIdentityColumn", result.Method);
Assert.Equal("SqlServerPropertyBuilderExtensions", result.DeclaringType);

Assert.Collection(
result.Arguments,
Expand All @@ -225,6 +228,7 @@ public void GenerateFluentApi_IModel_works_with_HiLo()
var result = generator.GenerateFluentApiCalls((IModel)modelBuilder.Model, annotations).Single();

Assert.Equal("UseHiLo", result.Method);
Assert.Equal("SqlServerModelBuilderExtensions", result.DeclaringType);

Assert.Collection(
result.Arguments,
Expand All @@ -244,6 +248,7 @@ public void GenerateFluentApi_IProperty_works_with_HiLo()
var result = generator.GenerateFluentApiCalls((IProperty)property, annotations).Single();

Assert.Equal("UseHiLo", result.Method);
Assert.Equal("SqlServerPropertyBuilderExtensions", result.DeclaringType);

Assert.Collection(
result.Arguments,
Expand Down

0 comments on commit 60ec60b

Please sign in to comment.