Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's not possible to customize the foreign key constraint name with data annotations or <code>DbModelBuilder</code> Fluent API. However, you can control the name with code-based migrations.</p> <ul> <li><p>First option: When the tables get created via migrations:</p> <p>The migration code that gets automatically generated for the join table would look like this:</p> <pre><code>public partial class MyMigration : DbMigration { public override void Up() { CreateTable("GroupUser", c =&gt; new { UserId = c.Int(nullable: false), GroupId = c.Int(nullable: false), }) .PrimaryKey(t =&gt; new { t.UserId, t.GroupId }) .ForeignKey("User", t =&gt; t.UserId, cascadeDelete: true) .ForeignKey("Group", t =&gt; t.GroupId, cascadeDelete: true) .Index(t =&gt; t.UserId) .Index(t =&gt; t.GroupId); // ... } } </code></pre> <p>Here you can modify the two <code>ForeignKey</code> method calls to set a custom constraint name before you call <code>update-database</code>:</p> <pre><code> .ForeignKey("User", t =&gt; t.UserId, cascadeDelete: true, name: "FK_GroupUser_UserId") .ForeignKey("Group", t =&gt; t.GroupId, cascadeDelete: true, name: "FK_GroupUser_GroupId") </code></pre></li> <li><p>Second option: When the tables already exist you can drop the constraint and add a new renamed one in a migration:</p> <pre><code>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") // ... } } </code></pre></li> </ul>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload