From 60ec60b6707baedfdede2a22fd3678dad3d0b0c0 Mon Sep 17 00:00:00 2001 From: Shay Rojansky Date: Wed, 22 Sep 2021 11:45:52 +0300 Subject: [PATCH] Fix snapshot generation with HiLo Fixes #26134 --- .../SqlServerAnnotationCodeGenerator.cs | 6 ++- .../Migrations/ModelSnapshotSqlServerTest.cs | 40 +++++++++++++++++++ .../SqlServerAnnotationCodeGeneratorTest.cs | 5 +++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/EFCore.SqlServer/Design/Internal/SqlServerAnnotationCodeGenerator.cs b/src/EFCore.SqlServer/Design/Internal/SqlServerAnnotationCodeGenerator.cs index 4b2d1734034..a34ec2eb1e1 100644 --- a/src/EFCore.SqlServer/Design/Internal/SqlServerAnnotationCodeGenerator.cs +++ b/src/EFCore.SqlServer/Design/Internal/SqlServerAnnotationCodeGenerator.cs @@ -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)); @@ -374,7 +378,7 @@ protected override bool IsHandledByConvention(IModel model, IAnnotation annotati var name = GetAndRemove(annotations, SqlServerAnnotationNames.HiLoSequenceName); var schema = GetAndRemove(annotations, SqlServerAnnotationNames.HiLoSequenceSchema); return new( - _modelUseHiLoMethodInfo, + onModel ? _modelUseHiLoMethodInfo : _propertyUseHiLoMethodInfo, (name, schema) switch { (null, null) => Array.Empty(), diff --git a/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs b/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs index 326217e2319..df48756c696 100644 --- a/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs +++ b/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs @@ -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(); + builder.Ignore(); + }, + 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(""Id"") + .ValueGeneratedOnAdd() + .HasColumnType(""int""); + + SqlServerPropertyBuilderExtensions.UseHiLo(b.Property(""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() { diff --git a/test/EFCore.SqlServer.Tests/Design/Internal/SqlServerAnnotationCodeGeneratorTest.cs b/test/EFCore.SqlServer.Tests/Design/Internal/SqlServerAnnotationCodeGeneratorTest.cs index a9254179ed5..80a4d039abc 100644 --- a/test/EFCore.SqlServer.Tests/Design/Internal/SqlServerAnnotationCodeGeneratorTest.cs +++ b/test/EFCore.SqlServer.Tests/Design/Internal/SqlServerAnnotationCodeGeneratorTest.cs @@ -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, @@ -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, @@ -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, @@ -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, @@ -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,