2016-04-26 101 views
1
  1. same question有人问,12天后还没有得到答复...更改标识3.0表名不工作

  2. 我已经看过this它采用“ToTable”为该问题的更新。

  3. 我看过this这似乎已过时。

我想更改标识3.0表的表名 - ASP.NET Core。

到目前为止,使用“ToTable”选项(上面的第2项)更新,我设法破坏了我的锁文件,并因此破坏了项目。第三个选项已过时。

我创建了一个香草项目 - 不只是通过VS2015创建了身份3.0

我再尝试这样的变化:

protected override void OnModelCreating(ModelBuilder builder) 
    { 
     base.OnModelCreating(builder); 
     // Customize the ASP.NET Identity model and override the defaults if needed. 
     // For example, you can rename the ASP.NET Identity table names and more. 
     // Add your customizations after calling base.OnModelCreating(builder); 

     builder.Entity<IdentityUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId"); 
     builder.Entity<ApplicationUser>().ToTable("MyUsers").Property(p => p.Id).HasColumnName("UserId"); 
     builder.Entity<IdentityRole>().ToTable("MyRoles"); 
    } 

我再检查更新的迁移,看看表的名称发生了变化,他们没有。

此刻我正在使用1.0.0-rc1-update2。

如何更改这些表的名称?

+1

可能重复http://stackoverflow.com/questions/34523066/how-can-i-change-the-table-names -used-asp-net-identity-3-vnext – firste

+0

是的..错过了那一个。我有这个工作,但我无法得到“builder.Entity >()。ForSqlServerToTable(”RoleClaims“);”上班。所有其他人的工作,但是当我添加它在它没有.. – si2030

回答

6

尝试此代码,它将所有asp.net标识的默认表名称更改为自定义表名称,例如用户,角色,UserClaim等等,它适用于我。

using Microsoft.AspNetCore.Identity; 
... 
public class ApplicationDbContext : IdentityDbContext<ApplicationUser> 
{ 

    ... 

    protected override void OnModelCreating(ModelBuilder modelBuilder) 
    { 
     base.OnModelCreating(modelBuilder); 
     // Add your customizations after calling base.OnModelCreating(modelBuilder); 
     modelBuilder.Entity<ApplicationUser>().ToTable("User"); 
     modelBuilder.Entity<IdentityRole>().ToTable("Role"); 
     modelBuilder.Entity<IdentityUserClaim<string>>().ToTable("UserClaim"); 
     modelBuilder.Entity<IdentityUserRole<string>>().ToTable("UserRole"); 
     modelBuilder.Entity<IdentityUserLogin<string>>().ToTable("UserLogin"); 
     modelBuilder.Entity<IdentityRoleClaim<string>>().ToTable("RoleClaim"); 
     modelBuilder.Entity<IdentityUserToken<string>>().ToTable("UserToken"); 
     } 

} 
2

您需要包含所有的泛型类型参数才能正常工作。您还再需要改变用户和角色,包括通用型ARGS太

public class BlahDbContext : IdentityDbContext<User, Role, long, UserClaim, UserRole, UserLogin, RoleClaim, UserToken> 
{ 
    public BlahDbContext(DbContextOptions<BlahDbContext> options) 
     : base(options) 
    { 
    } 

    protected override void OnModelCreating(ModelBuilder builder) 
    { 
     base.OnModelCreating(builder); 
     // Customize the ASP.NET Identity model and override the defaults if needed. 
     // For example, you can rename the ASP.NET Identity table names and more. 
     // Add your customizations after calling base.OnModelCreating(builder); 

     builder.Entity<User>().ToTable("Users"); 
     builder.Entity<Role>().ToTable("Roles"); 
     builder.Entity<UserRole>().ToTable("UserRoles"); 
     builder.Entity<UserLogin>().ToTable("UserLogins"); 
     builder.Entity<UserClaim>().ToTable("UserClaims"); 

     builder.Entity<RoleClaim>().ToTable("RoleClaims"); 
     builder.Entity<UserToken>().ToTable("UserTokens");    
    } 
} 

public class User : IdentityUser<long, UserClaim, UserRole, UserLogin> 
public class Role : IdentityRole<long, UserRole, RoleClaim>