EF7 身份自定义 table 名称第一种子

EF7 Identity custom table name first seed

我已经创建了一个新的干净的 asp.net 5 项目 (rc1-final),我只需要更改默认的 ef 身份 table 名称。

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    protected override void OnModelCreating(ModelBuilder builder)
    {
        // On event model creating
        base.OnModelCreating(builder);

        // Define table name
        builder.Entity<IdentityUser>().ToTable("BackEnd_AspNetUsers").Property(p => p.Id).HasColumnName("UserId");
        builder.Entity<ApplicationUser>().ToTable("BackEnd_AspNetUsers").Property(p => p.Id).HasColumnName("UserId");
        builder.Entity<IdentityUserRole<string>>().ToTable("BackEnd_AspNetUserRoles");
        builder.Entity<IdentityUserLogin<string>>().ToTable("BackEnd_AspNetUserLogins");
        builder.Entity<IdentityUserClaim<string>>().ToTable("BackEnd_AspNetUserClaims");
        builder.Entity<IdentityRole>().ToTable("BackEnd_AspNetRoles");
    }
}

我收到以下错误

InvalidOperationException: Cannot use table 'BackEnd_AspNetUsers' in schema '' for entity 'ApplicationUser' since it is being used for another entity.

此处的这些行表明您正在尝试为基本标识 类 和您的应用程序的继承版本设置映射 类;

builder.Entity<IdentityUser>().ToTable("BackEnd_AspNetUsers").Property(p => p.Id).HasColumnName("UserId");
builder.Entity<ApplicationUser>().ToTable("BackEnd_AspNetUsers").Property(p => p.Id).HasColumnName("UserId");

你不需要两者,你应该只指定继承的一个 - ApplicationUser

在您的情况下,您应该使用 ForSqlServerToTable("newtablename")

builder.Entity<IdentityUser>().ForSqlServerToTable("BackEnd_AspNetUsers").Property(p => p.Id).HasColumnName("UserId");
builder.Entity<ApplicationUser>().ForSqlServerToTable("BackEnd_AspNetUsers").Property(p => p.Id).HasColumnName("UserId");