efcore icon indicating copy to clipboard operation
efcore copied to clipboard

Renaming Identity Tables and Foreign Keys

Open dnlruzzo opened this issue 11 years ago • 2 comments

When trying to rename the identity tables using rc1 a bad migration was generated, including new column and foreign key as described below.

I've tested using beta7 and beta8 and it worked well.

Steps to reproduce:

  • With VS 2015 updated to rc1, scaffold an app using identity.
  • Remove any generated migration and model snapshot.
  • Override the IdentityDbContext method OnModelCreating with the following code, for brevity:
builder
    .Entity<IdentityRole>(table =>
    {
        table.ToTable("Role");

        table
            .HasKey(k => k.Id)
            .HasName("PK_Role");

        table
            .HasIndex(i => i.NormalizedName)
            .HasName("IX_Role_NormalizedName");
    })
    .Entity<IdentityRoleClaim<string>>(table =>
    {
        table.ToTable("RoleClaim");

        table
            .HasOne<IdentityRole>()
            .WithMany()
            .HasForeignKey(f => f.RoleId)
            .HasConstraintName("FK_RoleClaim_Role_RoleId");
    });
  • Add new migration and will be possible to visualize the new column IdentityRoleId and foreign key configuration for this column:
migrationBuilder.CreateTable(
    name: "RoleClaim",
    columns: table => new
    {
        ...omitted for brevity
        IdentityRoleId = table.Column<string>(nullable: true),
        RoleId = table.Column<string>(nullable: false)
    },
    constraints: table =>
    {
        ...omitted for brevity        
        table.ForeignKey(
            name: "FK_IdentityRoleClaim<string>_IdentityRole_IdentityRoleId",
            column: x => x.IdentityRoleId,
            principalTable: "Role",
            principalColumn: "Id",
            onDelete: ReferentialAction.Restrict);
        table.ForeignKey(
            name: "FK_RoleClaim_Role_RoleId",
            column: x => x.RoleId,
            principalTable: "Role",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);
    });

dnlruzzo avatar Nov 24 '15 00:11 dnlruzzo

The base.OnModelCreating method has following configuration in it.

builder.Entity<TRole>(b =>
            {
                .....
                b.HasMany(r => r.Claims).WithOne().HasForeignKey(rc => rc.RoleId).IsRequired();
            });

Which configures following navigation from IdentityRole

public virtual ICollection<IdentityRoleClaim<TKey>> Claims { get; } = new List<IdentityRoleClaim<TKey>>();

Thing to notice here is that, the overridden configuration does not have any navigation configured, hence it is different relationship for EF. Since the both the configuration tries to use the same foreign key property, the last one wins (overridden configuration) and will have RoleId as foreign key property. For the other relationship defined in base method, EF will create shadow property for fk which is named as IdentityRoleId (Target entity type name + pk name).

If you intend to rename the foreign key constraint name only then the relationship configuration should been following:

table.HasOne<IdentityRole>().WithMany(r => r.Claims).HasForeignKey(rc => rc.RoleId).HasConstraintName("FK_RoleClaim_Role_RoleId");

Hope this helps.

smitpatel avatar Nov 24 '15 08:11 smitpatel

Thank you Smit Patel, it's helped a lot.

dnlruzzo avatar Nov 24 '15 12:11 dnlruzzo