重命名默认表但AspNetusers表仍在创建

时间:2017-01-04 17:10:04

标签: asp.net asp.net-identity

我正在使用ASP.NET身份。我使用以下代码重命名了默认表。

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<IdentityUser>().ToTable("Users");
        modelBuilder.Entity<IdentityRole>().ToTable("Roles");
        modelBuilder.Entity<IdentityUserRole>().ToTable("UserRoles");
        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaims");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogins");
    }
}

}

但是当我运行我的应用程序时,仍然创建了AspnetUsers但是只有一个名为“Id”的列。正在填充此表以及新的“用户”表。为什么?我该如何阻止这种行为。

我在这里看到了同样的问题,但没有得到好的答复:Identity 2.0 Code First Table Renaming

我希望我没有违反StackOverflow的指导原则来重新提出这个问题///如果事先道歉的话,那么这就好了!

1 个答案:

答案 0 :(得分:-1)

在寻找快速解决方案时偶然发现了这篇文章 - 现在我已经挖了一些代码并找到答案;也许它有助于某人。

我想你已经覆盖了默认的IdentityUser类,对吧?

public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
...

在这种情况下,您必须从模型构建器中排除默认的IdentityUser:

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        //modelBuilder.Entity<IdentityUser>().ToTable("Users", "dbo"); // we have overwritten IdentityUser, so no more need for the empty AspNetUsers table
        modelBuilder.Entity<ApplicationUser>().ToTable("Users", "dbo"); // Use ApplicationUser instead
        modelBuilder.Entity<IdentityRole>().ToTable("Roles", "dbo");