Skip to content

Did the naming convention change for HasCheckConstraint in v.6? Is this a breaking change? #27059

Closed

Description

@davemateer

Reproduce:

using Microsoft.EntityFrameworkCore;

namespace MigrationTest
{
    public sealed class Record
    {
        public int Id { get; set; }
    }

    public sealed class ApplicationDbContext : DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=test;Trusted_Connection=True;MultipleActiveResultSets=true");
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<Record>().HasCheckConstraint("CHK_Records_Id", "[Id] > 10");
        }

        public DbSet<Record> Records { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

When you generate a migration using dotnet ef migrations add foo using .Net 5 and EF Core 5.0.13 (libraries and tool), the migration output includes the following:

table.CheckConstraint("CHK_Records_Id", "[Id] > 10");

And the snapshot:

b.HasCheckConstraint("CHK_Records_Id", "[Id] > 10");

When you change everything to Net 6 and EF Core 6.0.0 (libraries and tool), those change to the following:

table.CheckConstraint("CK_Records_CHK_Records_Id", "[Id] > 10");

Snapshot:

b.HasCheckConstraint("CHK_Records_Id", "[Id] > 10");

Is this expected, or is this a bug?

Activity

  1. ajcvickers commented on Jan 6, 2022

    @ajcvickers
    Contributor

    Note for triage: verified that the constraint name is changed to "CK_Records_CHK_Records_Id" in 6.0.

    Migration with 5.0:

    using Microsoft.EntityFrameworkCore.Migrations;
    
    namespace FiveOh.Migrations
    {
        public partial class One : Migration
        {
            protected override void Up(MigrationBuilder migrationBuilder)
            {
                migrationBuilder.CreateTable(
                    name: "Records",
                    columns: table => new
                    {
                        Id = table.Column<int>(type: "int", nullable: false)
                            .Annotation("SqlServer:Identity", "1, 1")
                    },
                    constraints: table =>
                    {
                        table.PrimaryKey("PK_Records", x => x.Id);
                        table.CheckConstraint("CHK_Records_Id", "[Id] > 10");
                    });
            }
    
            protected override void Down(MigrationBuilder migrationBuilder)
            {
                migrationBuilder.DropTable(
                    name: "Records");
            }
        }
    }

    Migration with 6.0:

    using Microsoft.EntityFrameworkCore.Migrations;
    
    #nullable disable
    
    namespace Migrations
    {
        public partial class One : Migration
        {
            protected override void Up(MigrationBuilder migrationBuilder)
            {
                migrationBuilder.CreateTable(
                    name: "Records",
                    columns: table => new
                    {
                        Id = table.Column<int>(type: "int", nullable: false)
                            .Annotation("SqlServer:Identity", "1, 1")
                    },
                    constraints: table =>
                    {
                        table.PrimaryKey("PK_Records", x => x.Id);
                        table.CheckConstraint("CK_Records_CHK_Records_Id", "[Id] > 10");
                    });
            }
    
            protected override void Down(MigrationBuilder migrationBuilder)
            {
                migrationBuilder.DropTable(
                    name: "Records");
            }
        }
    }
  2. AndriySvyryd commented on Jan 6, 2022

    @AndriySvyryd
    Member

    This is a known breaking change: https://docs.microsoft.com/en-us/ef/core/what-is-new/ef-core-6.0/breaking-changes#unique-check-constraints
    The store name needs to be specified using HasName:
    modelBuilder.Entity<Record>().HasCheckConstraint("Id", "[Id] > 10", c => c.HasName("CHK_Records_Id"));

  3. davemateer commented on Jan 7, 2022

    @davemateer
    Author

    Ah, thanks. Sorry, I missed that in the docs. I even saw that section but I think my brain shut off because I wasn't doing anything that was related to uniqueness, so it didn't register that that mitigation applied.

    At the end of the day, in our actual application, we're using IMutableEntityType.AddCheckConstraint which doesn't seem to have an overload that supports overriding the name like HasCheckConstraint does. (The name parameter passed to AddCheckConstraint is still going through the convention mutation apparently.) We ended up just going the "when in Rome" route and changed the name of all our constraints, but it's good to know this is expected behavior at least. Thanks for pointing that out.

  4. added this to the 6.0.x milestone on Jan 7, 2022
  5. added a commit that references this issue on Jan 7, 2022
    72fca6c
  6. AndriySvyryd commented on Jan 7, 2022

    @AndriySvyryd
    Member

    @davemateer You can set IMutableCheckConstraint.Name, the name passed to AddCheckConstraint is actually ModelName.

    We discussed this again and decided to revert the prefixing change, as it's unexpected and unnecessary.

  7. added 4 commits that reference this issue on Jan 7, 2022
    e2b7133
    ca2eae4
    1c5f59e
    16e1795
  8. added a commit that references this issue on Jan 10, 2022
  9. modified the milestones: 6.0.x, 6.0.2 on Jan 10, 2022
  10. added a commit that references this issue on Jan 10, 2022
  11. removed their assignment
    on Mar 15, 2024
  12. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions