Skip to content

Commit

Permalink
Stop prefixing check constraints.
Browse files Browse the repository at this point in the history
Fixes #27059
  • Loading branch information
AndriySvyryd committed Jan 7, 2022
1 parent 5f90495 commit 1c5f59e
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/EFCore.Relational/Metadata/IReadOnlyCheckConstraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ public virtual void CheckConstraint_is_stored_in_snapshot_as_fluent_api()
builder =>
{
builder.Entity<EntityWithTwoProperties>()
.HasCheckConstraint("CK_Customer_AlternateId", "AlternateId > Id", ck => ck.HasName("CK_Customer_AlternateId"));
.HasCheckConstraint("AlternateId", "AlternateId > Id", ck => ck.HasName("CK_Customer_AlternateId"));
builder.Ignore<EntityWithOneProperty>();
},
AddBoilerPlate(
Expand All @@ -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 =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -641,7 +641,7 @@ public virtual Task Add_column_with_check_constraint()
"People", e =>
{
e.Property<int>("DriverLicense");
e.HasCheckConstraint("Foo", $"{DelimitIdentifier("DriverLicense")} > 0");
e.HasCheckConstraint("CK_People_Foo", $"{DelimitIdentifier("DriverLicense")} > 0");
}),
model =>
{
Expand Down Expand Up @@ -1307,7 +1307,7 @@ public virtual Task Add_check_constraint_with_name()
e.Property<int>("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
Expand All @@ -1322,8 +1322,8 @@ public virtual Task Alter_check_constraint()
e.Property<int>("Id");
e.Property<int>("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
Expand All @@ -1338,7 +1338,7 @@ public virtual Task Drop_check_constraint()
e.Property<int>("Id");
e.Property<int>("DriverLicense");
}),
builder => builder.Entity("People").HasCheckConstraint("Foo", $"{DelimitIdentifier("DriverLicense")} > 0"),
builder => builder.Entity("People").HasCheckConstraint("CK_People_Foo", $"{DelimitIdentifier("DriverLicense")} > 0"),
builder => { },
model =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -475,9 +475,9 @@ public virtual void Passes_for_incompatible_uniquified_check_constraints_with_sh
var modelBuilder = CreateConventionalModelBuilder();

modelBuilder.Entity<A>().HasOne<B>().WithOne(b => b.A).HasForeignKey<A>(a => a.Id).HasPrincipalKey<B>(b => b.Id).IsRequired();
modelBuilder.Entity<A>().HasCheckConstraint("SomeCK", "Id > 0");
modelBuilder.Entity<A>().HasCheckConstraint("CK_Table_SomeCK", "Id > 0");
modelBuilder.Entity<A>().ToTable("Table");
modelBuilder.Entity<B>().HasCheckConstraint("SomeCK", "Id > 10");
modelBuilder.Entity<B>().HasCheckConstraint("CK_Table_SomeCK", "Id > 10");
modelBuilder.Entity<B>().ToTable("Table");

var model = Validate(modelBuilder);
Expand All @@ -492,9 +492,9 @@ public virtual void Passes_for_compatible_shared_check_constraints_with_shared_t
var modelBuilder = CreateConventionalModelBuilder();

modelBuilder.Entity<A>().HasOne<B>().WithOne(b => b.A).HasForeignKey<A>(a => a.Id).HasPrincipalKey<B>(b => b.Id).IsRequired();
modelBuilder.Entity<A>().HasCheckConstraint("SomeCK", "Id > 0");
modelBuilder.Entity<A>().HasCheckConstraint("CK_Table_SomeCK", "Id > 0");
modelBuilder.Entity<A>().ToTable("Table");
modelBuilder.Entity<B>().HasCheckConstraint("SomeCK", "Id > 0");
modelBuilder.Entity<B>().HasCheckConstraint("CK_Table_SomeCK", "Id > 0");
modelBuilder.Entity<B>().ToTable("Table");

var model = Validate(modelBuilder);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ public void Create_table()
x.Property<int?>("ParentAltId");
x.HasOne("Node").WithMany().HasForeignKey("ParentAltId");
x.HasIndex("ParentAltId");
x.HasCheckConstraint("SomeCheckConstraint", "[Id] > 10");
x.HasCheckConstraint("CK_Node_SomeCheckConstraint", "[Id] > 10");
}),
upOps =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,10 @@ public void Can_add_check_constraints()
var modelBuilder = CreateModelBuilder();
modelBuilder.Entity<Child>()
.HasBaseType(null)
.HasCheckConstraint("LargeId", "Id > 1000", c => c.HasName("CK_LargeId"));
.HasCheckConstraint("CK_ChildBase_LargeId", "Id > 1000", c => c.HasName("CK_LargeId"));
modelBuilder.Entity<ChildBase>()
.HasCheckConstraint("PositiveId", "Id > 0")
.HasCheckConstraint("LargeId", "Id > 1000");
.HasCheckConstraint("CK_ChildBase_LargeId", "Id > 1000");
modelBuilder.Entity<Child>()
.HasBaseType<ChildBase>();
modelBuilder.Entity<DisjointChildSubclass1>();
Expand All @@ -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);

Expand Down

0 comments on commit 1c5f59e

Please sign in to comment.