EF代码第一 - 自定义外键约束名称 [英] EF code first - custom foreign key constraint name

查看:210
本文介绍了EF代码第一 - 自定义外键约束名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有机会在使用代码时修改Entity Framework生成的外键名称。



我有两个实体 - 用户 - 具有多对多关系,因此有一个关联表 GroupUser 。不幸的是,自动生成的外键约束被命名为 FK_dbo.GroupUser_dbo.User_User_UserId FK_dbo.GroupUser_dbo.Group_Group_GroupId 。 p>

我想要调用外键约束,如 FK_GroupUser_UserId FK_GroupUser_GroupId 。这对我来说看起来更清洁。

解决方案

不可能使用数据注释或<$ c $来自定义外键约束名称c> DbModelBuilder 流畅的API。但是,您可以使用基于代码的迁移来控制名称。




  • 第一个选项:通过迁移创建表: / p>

    为连接表自动生成的迁移代码如下所示:

      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 =>新的{t.UserId,t.GroupId})
    .ForeignKey(User,t => t.UserId,cascadeDelete:true)
    .ForeignKey(Group,t => t $ G
    .Index

    // ...
    }
    }

    在这里您可以修改两个 ForeignKey 方法调用以在调用 update-database 之前设置自定义约束名称:

      .ForeignKey(User,t => t.UserId,cascadeDelete:true,
    name: FK_GroupUser_UserId)
    .ForeignKey(Group,t => t.GroupId,cascadeDelete:true,
    name:FK_GroupUser_GroupId)


  • 第二个选项:当表已存在时,您可以删除约束并在迁移中添加一个新的重命名:

      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,
    名称:FK_GroupUser_GroupId)

    // ...
    }



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.

解决方案

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")
    
            // ...
        }
    }
    

这篇关于EF代码第一 - 自定义外键约束名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆