Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
876 views
in Technique[技术] by (71.8m points)

entity framework 5 - EF code first - custom foreign key constraint name

I wonder if there is any chance to change name of foreign key constraint generated by Entity Framework when using code first.

I have two entities - User and Group - with many-to-many relationship so there is an association table GroupUser. Unfortunately, auto-generated foreign key constraints are named FK_dbo.GroupUser_dbo.User_User_UserId and FK_dbo.GroupUser_dbo.Group_Group_GroupId.

I would like to have foreign key constraints called like FK_GroupUser_UserId and FK_GroupUser_GroupId. That looks much cleaner to me.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's not possible to customize the foreign key constraint name with data annotations or DbModelBuilder Fluent API. However, you can control the name with code-based migrations.

  • First option: When the tables get created via migrations:

    The migration code that gets automatically generated for the join table would look like this:

    public partial class MyMigration : DbMigration
    {
        public override void Up()
        {
            CreateTable("GroupUser",
                c => new
                {
                    UserId = c.Int(nullable: false),
                    GroupId = c.Int(nullable: false),
                })
            .PrimaryKey(t => new { t.UserId, t.GroupId })
            .ForeignKey("User", t => t.UserId, cascadeDelete: true)
            .ForeignKey("Group", t => t.GroupId, cascadeDelete: true)
            .Index(t => t.UserId)
            .Index(t => t.GroupId);
    
            // ...
        }
    }
    

    Here you can modify the two ForeignKey method calls to set a custom constraint name before you call update-database:

            .ForeignKey("User", t => t.UserId, cascadeDelete: true,
                name: "FK_GroupUser_UserId")
            .ForeignKey("Group", t => t.GroupId, cascadeDelete: true,
                name: "FK_GroupUser_GroupId")
    
  • Second option: When the tables already exist you can drop the constraint and add a new renamed one in a migration:

    public partial class MyMigration : DbMigration
    {
        public override void Up()
        {
            DropForeignKey("UserGroup", "UserId", "User");
            DropForeignKey("UserGroup", "GroupId", "Group");
    
            AddForeignKey("UserGroup", "UserId", "User",
                name: "FK_GroupUser_UserId");
            AddForeignKey("UserGroup", "GroupId", "Group",
                name: "FK_GroupUser_GroupId")
    
            // ...
        }
    }
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...