.net核心身份更改表名称

时间:2017-04-29 09:22:14

标签: c# .net asp.net-core

当我尝试重命名表的名称时,我使用下面的代码。一切都很好。但是当我更新数据库时,它有一些旧表。例如: AspNetUserRoles和新表UserRoles。为什么会这样?感谢

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);

    // Customizations
    builder.Entity<User>(i =>
    {
        i.ToTable("Users");
        i.HasKey(x => x.Id);
    });
    builder.Entity<IdentityRole>(i =>
    {
        i.ToTable("Roles");
        i.HasKey(x => x.Id);
    });
    builder.Entity<IdentityUserRole<Guid>>(i =>
    {
        i.ToTable("UserRoles");
        i.HasKey(x => new { x.RoleId, x.UserId });
    });
    builder.Entity<IdentityUserLogin<Guid>>(i =>
    {
        i.ToTable("UserLogins");
        i.HasKey(x => new { x.ProviderKey, x.LoginProvider });
    });
    builder.Entity<IdentityRoleClaim<Guid>>(i =>
    {
        i.ToTable("RoleClaims");
        i.HasKey(x => x.Id);
    });
    builder.Entity<IdentityUserClaim<Guid>>(i =>
    {
        i.ToTable("UserClaims");
        i.HasKey(x => x.Id);
    });
    builder.Entity<IdentityUserToken<Guid>>(i =>
    {
        i.ToTable("UserTokens");
        i.HasKey(x => x.UserId);
    });
}

1 个答案:

答案 0 :(得分:1)

我就是这样做的,它可以在迁移/更新时正常工作。

base.OnModelCreating(builder);

builder.Entity<User>().ToTable("Users");
builder.Entity<IdentityRole>().ToTable("Roles");
builder.Entity<IdentityUserRole<string>>().ToTable("UserRoles");
builder.Entity<IdentityUserClaim<string>>().ToTable("UserClaims");
builder.Entity<IdentityUserLogin<string>>().ToTable("UserLogins");
builder.Entity<IdentityRoleClaim<string>>().ToTable("RoleClaims");
builder.Entity<IdentityUserToken<string>>().ToTable("UserTokens");

另请注意,我不使用Guid类型。 Microsoft使用string作为其Id列,因此我们也会使用它。