【问题标题】:Renamed default tables but AspNetusers Table Still Getting Created重命名默认表,但 AspNetusers 表仍在创建中
【发布时间】:2017-03-26 21:12:53
【问题描述】:

我正在使用 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 的指导方针/// 如果提前道歉!

【问题讨论】:

    标签: asp.net asp.net-identity


    【解决方案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");
    

    【讨论】:

      猜你喜欢
      • 2021-06-05
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 2020-05-31
      • 2023-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多