From 1c5f59ecb92851b39c480965eb8e2999bc416fe1 Mon Sep 17 00:00:00 2001 From: Andriy Svyryd Date: Fri, 7 Jan 2022 10:56:31 -0800 Subject: [PATCH] Stop prefixing check constraints. Fixes #27059 --- .../Metadata/IReadOnlyCheckConstraint.cs | 3 ++- .../Migrations/ModelSnapshotSqlServerTest.cs | 4 ++-- .../Migrations/MigrationsTestBase.cs | 12 ++++++------ .../Infrastructure/RelationalModelValidatorTest.cs | 8 ++++---- .../Migrations/Internal/MigrationsModelDifferTest.cs | 2 +- .../SqlServerModelBuilderGenericTest.cs | 10 +++++----- 6 files changed, 20 insertions(+), 19 deletions(-) diff --git a/src/EFCore.Relational/Metadata/IReadOnlyCheckConstraint.cs b/src/EFCore.Relational/Metadata/IReadOnlyCheckConstraint.cs index c37e6a5f3b8..7fe71840830 100644 --- a/src/EFCore.Relational/Metadata/IReadOnlyCheckConstraint.cs +++ b/src/EFCore.Relational/Metadata/IReadOnlyCheckConstraint.cs @@ -50,7 +50,8 @@ string GetDefaultName(in StoreObjectIdentifier storeObject) { var prefix = $"CK_{storeObject.Name}_"; return Uniquifier.Truncate( - ModelName.StartsWith(prefix, StringComparison.Ordinal) + !(AppContext.TryGetSwitch("Microsoft.EntityFrameworkCore.Issue27059", out var enabled) && enabled) + || ModelName.StartsWith(prefix, StringComparison.Ordinal) ? ModelName : prefix + ModelName, EntityType.Model.GetMaxIdentifierLength()); diff --git a/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs b/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs index cc2a489890e..d2041db593e 100644 --- a/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs +++ b/test/EFCore.Design.Tests/Migrations/ModelSnapshotSqlServerTest.cs @@ -711,7 +711,7 @@ public virtual void CheckConstraint_is_stored_in_snapshot_as_fluent_api() builder => { builder.Entity() - .HasCheckConstraint("CK_Customer_AlternateId", "AlternateId > Id", ck => ck.HasName("CK_Customer_AlternateId")); + .HasCheckConstraint("AlternateId", "AlternateId > Id", ck => ck.HasName("CK_Customer_AlternateId")); builder.Ignore(); }, AddBoilerPlate( @@ -732,7 +732,7 @@ public virtual void CheckConstraint_is_stored_in_snapshot_as_fluent_api() b.ToTable(""EntityWithTwoProperties""); - b.HasCheckConstraint(""CK_Customer_AlternateId"", ""AlternateId > Id"", c => c.HasName(""CK_Customer_AlternateId"")); + b.HasCheckConstraint(""AlternateId"", ""AlternateId > Id"", c => c.HasName(""CK_Customer_AlternateId"")); });"), o => { diff --git a/test/EFCore.Relational.Specification.Tests/Migrations/MigrationsTestBase.cs b/test/EFCore.Relational.Specification.Tests/Migrations/MigrationsTestBase.cs index 1b240ecd7c4..750c93f17b4 100644 --- a/test/EFCore.Relational.Specification.Tests/Migrations/MigrationsTestBase.cs +++ b/test/EFCore.Relational.Specification.Tests/Migrations/MigrationsTestBase.cs @@ -89,7 +89,7 @@ await Test( e.HasKey("CustomId"); e.HasAlternateKey("SSN"); - e.HasCheckConstraint("EmployerId", $"{DelimitIdentifier("EmployerId")} > 0"); + e.HasCheckConstraint("CK_People_EmployerId", $"{DelimitIdentifier("EmployerId")} > 0"); e.HasOne("Employers").WithMany("People").HasForeignKey("EmployerId"); e.HasComment("Table comment"); @@ -641,7 +641,7 @@ public virtual Task Add_column_with_check_constraint() "People", e => { e.Property("DriverLicense"); - e.HasCheckConstraint("Foo", $"{DelimitIdentifier("DriverLicense")} > 0"); + e.HasCheckConstraint("CK_People_Foo", $"{DelimitIdentifier("DriverLicense")} > 0"); }), model => { @@ -1307,7 +1307,7 @@ public virtual Task Add_check_constraint_with_name() e.Property("DriverLicense"); }), builder => { }, - builder => builder.Entity("People").HasCheckConstraint("Foo", $"{DelimitIdentifier("DriverLicense")} > 0"), + builder => builder.Entity("People").HasCheckConstraint("CK_People_Foo", $"{DelimitIdentifier("DriverLicense")} > 0"), model => { // TODO: no scaffolding support for check constraints, https://github.com/aspnet/EntityFrameworkCore/issues/15408 @@ -1322,8 +1322,8 @@ public virtual Task Alter_check_constraint() e.Property("Id"); e.Property("DriverLicense"); }), - builder => builder.Entity("People").HasCheckConstraint("Foo", $"{DelimitIdentifier("DriverLicense")} > 0"), - builder => builder.Entity("People").HasCheckConstraint("Foo", $"{DelimitIdentifier("DriverLicense")} > 1"), + builder => builder.Entity("People").HasCheckConstraint("CK_People_Foo", $"{DelimitIdentifier("DriverLicense")} > 0"), + builder => builder.Entity("People").HasCheckConstraint("CK_People_Foo", $"{DelimitIdentifier("DriverLicense")} > 1"), model => { // TODO: no scaffolding support for check constraints, https://github.com/aspnet/EntityFrameworkCore/issues/15408 @@ -1338,7 +1338,7 @@ public virtual Task Drop_check_constraint() e.Property("Id"); e.Property("DriverLicense"); }), - builder => builder.Entity("People").HasCheckConstraint("Foo", $"{DelimitIdentifier("DriverLicense")} > 0"), + builder => builder.Entity("People").HasCheckConstraint("CK_People_Foo", $"{DelimitIdentifier("DriverLicense")} > 0"), builder => { }, model => { diff --git a/test/EFCore.Relational.Tests/Infrastructure/RelationalModelValidatorTest.cs b/test/EFCore.Relational.Tests/Infrastructure/RelationalModelValidatorTest.cs index add186ec9a9..ca123b5529a 100644 --- a/test/EFCore.Relational.Tests/Infrastructure/RelationalModelValidatorTest.cs +++ b/test/EFCore.Relational.Tests/Infrastructure/RelationalModelValidatorTest.cs @@ -475,9 +475,9 @@ public virtual void Passes_for_incompatible_uniquified_check_constraints_with_sh var modelBuilder = CreateConventionalModelBuilder(); modelBuilder.Entity().HasOne().WithOne(b => b.A).HasForeignKey(a => a.Id).HasPrincipalKey(b => b.Id).IsRequired(); - modelBuilder.Entity().HasCheckConstraint("SomeCK", "Id > 0"); + modelBuilder.Entity().HasCheckConstraint("CK_Table_SomeCK", "Id > 0"); modelBuilder.Entity().ToTable("Table"); - modelBuilder.Entity().HasCheckConstraint("SomeCK", "Id > 10"); + modelBuilder.Entity().HasCheckConstraint("CK_Table_SomeCK", "Id > 10"); modelBuilder.Entity().ToTable("Table"); var model = Validate(modelBuilder); @@ -492,9 +492,9 @@ public virtual void Passes_for_compatible_shared_check_constraints_with_shared_t var modelBuilder = CreateConventionalModelBuilder(); modelBuilder.Entity().HasOne().WithOne(b => b.A).HasForeignKey(a => a.Id).HasPrincipalKey(b => b.Id).IsRequired(); - modelBuilder.Entity().HasCheckConstraint("SomeCK", "Id > 0"); + modelBuilder.Entity().HasCheckConstraint("CK_Table_SomeCK", "Id > 0"); modelBuilder.Entity().ToTable("Table"); - modelBuilder.Entity().HasCheckConstraint("SomeCK", "Id > 0"); + modelBuilder.Entity().HasCheckConstraint("CK_Table_SomeCK", "Id > 0"); modelBuilder.Entity().ToTable("Table"); var model = Validate(modelBuilder); diff --git a/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs b/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs index d0adf9d008c..3c1c72d8af0 100644 --- a/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs +++ b/test/EFCore.Relational.Tests/Migrations/Internal/MigrationsModelDifferTest.cs @@ -443,7 +443,7 @@ public void Create_table() x.Property("ParentAltId"); x.HasOne("Node").WithMany().HasForeignKey("ParentAltId"); x.HasIndex("ParentAltId"); - x.HasCheckConstraint("SomeCheckConstraint", "[Id] > 10"); + x.HasCheckConstraint("CK_Node_SomeCheckConstraint", "[Id] > 10"); }), upOps => { diff --git a/test/EFCore.SqlServer.Tests/ModelBuilding/SqlServerModelBuilderGenericTest.cs b/test/EFCore.SqlServer.Tests/ModelBuilding/SqlServerModelBuilderGenericTest.cs index c53b5400060..633c22fa350 100644 --- a/test/EFCore.SqlServer.Tests/ModelBuilding/SqlServerModelBuilderGenericTest.cs +++ b/test/EFCore.SqlServer.Tests/ModelBuilding/SqlServerModelBuilderGenericTest.cs @@ -338,10 +338,10 @@ public void Can_add_check_constraints() var modelBuilder = CreateModelBuilder(); modelBuilder.Entity() .HasBaseType(null) - .HasCheckConstraint("LargeId", "Id > 1000", c => c.HasName("CK_LargeId")); + .HasCheckConstraint("CK_ChildBase_LargeId", "Id > 1000", c => c.HasName("CK_LargeId")); modelBuilder.Entity() .HasCheckConstraint("PositiveId", "Id > 0") - .HasCheckConstraint("LargeId", "Id > 1000"); + .HasCheckConstraint("CK_ChildBase_LargeId", "Id > 1000"); modelBuilder.Entity() .HasBaseType(); modelBuilder.Entity(); @@ -354,10 +354,10 @@ public void Can_add_check_constraints() var firstCheckConstraint = @base.FindCheckConstraint("PositiveId"); Assert.Equal("PositiveId", firstCheckConstraint.ModelName); Assert.Equal("Id > 0", firstCheckConstraint.Sql); - Assert.Equal("CK_ChildBase_PositiveId", firstCheckConstraint.Name); + Assert.Equal("PositiveId", firstCheckConstraint.Name); - var secondCheckConstraint = @base.FindCheckConstraint("LargeId"); - Assert.Equal("LargeId", secondCheckConstraint.ModelName); + var secondCheckConstraint = @base.FindCheckConstraint("CK_ChildBase_LargeId"); + Assert.Equal("CK_ChildBase_LargeId", secondCheckConstraint.ModelName); Assert.Equal("Id > 1000", secondCheckConstraint.Sql); Assert.Equal("CK_LargeId", secondCheckConstraint.Name);