"ef migrations add" always recreates foreign keys in the new migration

entity-framework-core

Question

I have RC1 with VS 2015 update 1 installed.

Whenever I try adding a new migration the same set of foreign keys get recreated in the Up method. Meaning they get dropped then directly added.

For example, when I add a migration without changing any of the models this gets generated (similar stuff is also generated in the Down method of course):

protected override void Up(MigrationBuilder migrationBuilder)
{
    migrationBuilder.DropForeignKey(name: "FK_IdentityRoleClaim<string>_IdentityRole_RoleId", table: "AspNetRoleClaims");
    migrationBuilder.DropForeignKey(name: "FK_IdentityUserClaim<string>_ApplicationUser_UserId", table: "AspNetUserClaims");
    migrationBuilder.DropForeignKey(name: "FK_IdentityUserLogin<string>_ApplicationUser_UserId", table: "AspNetUserLogins");
    migrationBuilder.DropForeignKey(name: "FK_IdentityUserRole<string>_IdentityRole_RoleId", table: "AspNetUserRoles");
    migrationBuilder.DropForeignKey(name: "FK_IdentityUserRole<string>_ApplicationUser_UserId", table: "AspNetUserRoles");
    migrationBuilder.AlterColumn<string>(
        name: "UserId",
        table: "AspNetUserLogins",
        nullable: false);
    migrationBuilder.AlterColumn<string>(
        name: "UserId",
        table: "AspNetUserClaims",
        nullable: false);
    migrationBuilder.AlterColumn<string>(
        name: "RoleId",
        table: "AspNetRoleClaims",
        nullable: false);
    migrationBuilder.AddForeignKey(
        name: "FK_IdentityRoleClaim<string>_IdentityRole_RoleId",
        table: "AspNetRoleClaims",
        column: "RoleId",
        principalTable: "AspNetRoles",
        principalColumn: "Id",
        onDelete: ReferentialAction.Cascade);
    migrationBuilder.AddForeignKey(
        name: "FK_IdentityUserClaim<string>_ApplicationUser_UserId",
        table: "AspNetUserClaims",
        column: "UserId",
        principalTable: "AspNetUsers",
        principalColumn: "Id",
        onDelete: ReferentialAction.Cascade);
    migrationBuilder.AddForeignKey(
        name: "FK_IdentityUserLogin<string>_ApplicationUser_UserId",
        table: "AspNetUserLogins",
        column: "UserId",
        principalTable: "AspNetUsers",
        principalColumn: "Id",
        onDelete: ReferentialAction.Cascade);
    migrationBuilder.AddForeignKey(
        name: "FK_IdentityUserRole<string>_IdentityRole_RoleId",
        table: "AspNetUserRoles",
        column: "RoleId",
        principalTable: "AspNetRoles",
        principalColumn: "Id",
        onDelete: ReferentialAction.Cascade);
    migrationBuilder.AddForeignKey(
        name: "FK_IdentityUserRole<string>_ApplicationUser_UserId",
        table: "AspNetUserRoles",
        column: "UserId",
        principalTable: "AspNetUsers",
        principalColumn: "Id",
        onDelete: ReferentialAction.Cascade);
}

This is completely useless, and it looks like it always happens in my case. I tried this in an aspnet5 application and also in a normal console app.

Accepted Answer

This is a known issue and it has already been fixed, the fix is coming for rc2. The issue was tracked here.

Thanks @firste.



Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why