ASP.NET 身份 2 table 映射

ASP.NET Identity 2 table mapping

用多个项目测试数据库。

第一个 MVC 项目使用 Identity 2 工作正常。构建了正确的 table,即 AspNetUsers、AspNetRoles 等

同一数据库上的第二个项目不工作,我很困惑为什么不工作。

途径是注册。

在我的 AccountController.cs 中,注册方法按预期调用并命中此代码:

var user = new ApplicationUser { UserName = model.ContactEmail, Email = model.ContactEmail };

使用 VS2015 调试,显示用户已完全填充。

想要使用独立身份 table,因此添加了以下代码,接下来会正确调用(在我的 IdentityModels.cs 文件中):

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<IdentityUser>().ToTable("sp_AspNetUsers","dbo");
        }

调试器没有任何投诉。

Returns 到 Account Controller.cs 中的下一行因此:

var result = await UserManager.CreateAsync(user, model.Password);

错误提示“无法将值 NULL 插入列 'UserName'、table 'dbName.dbo.AspNetUsers';”

错误似乎不言自明但是:

1) Why is it referencing the AspNetUsers table and not the sp_AspNetUsers table?

2) Debugging shows user.UserName has a value so why does it think it's NULL?

MVC 和 Identity 的新手,所以任何有助于我学习的指示都将不胜感激。


编辑

我现在将 IdentityModel.cs 中的代码修改为:

protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<IdentityUser>().ToTable("sp_AspNetUsers");
            modelBuilder.Entity<ApplicationUser>().ToTable("sp_AspNetUsers");
            modelBuilder.Entity<IdentityUserRole>().ToTable("sp_AspNetUserRoles");
            modelBuilder.Entity<IdentityUserLogin>().ToTable("sp_AspNetUserLogins");
            modelBuilder.Entity<IdentityUserClaim>().ToTable("sp_AspNetUserClaim");
            modelBuilder.Entity<IdentityRole>().ToTable("sp_AspNetRoles");
        }

我现在得到的错误是:

Invalid column name 'Discriminator'

在身份 table 的任何地方都看不到此列?

首先删除与 ASP 身份相关的所有表或重命名它们,以便稍后复制数据,然后尝试重新创建表,如果成功则恢复旧数据。