await UserManager.CreateAsync 寻找不存在的列

await UserManager.CreateAsync looking for column that does not exist

情况

我有一个我更新的工作程序需要更新方法中的 identity/login。所以我从 Identity 1 更新到 version2 并且我一直在修复错误,但我现在很困惑。

这是我的注册码

public async Task<ActionResult> Register(RegisterViewModel model)
{
    if (ModelState.IsValid)
    {
        //try
        //{
            var user = new ApplicationUser() { UserName = model.Email,Email = model.Email };
            var result = await UserManager.CreateAsync(user, model.Password);
            if (result.Succeeded)

问题

出现的问题是我在 var result = await UserManager.CreateAsync(user, model.Password); 行收到 Invalid column name 'UserId'
我不完全确定为什么会发生这种情况(或者这个 UserId 来自哪里,可能来自旧身份?)因为当我在行上放置断点时 SQL 将查询显示为

SELECT 
    [Extent1].[Id] AS [Id], 
    [Extent1].[Email] AS [Email], 
    [Extent1].[EmailConfirmed] AS [EmailConfirmed], 
    [Extent1].[PasswordHash] AS [PasswordHash], 
    [Extent1].[SecurityStamp] AS [SecurityStamp], 
    [Extent1].[PhoneNumber] AS [PhoneNumber], 
    [Extent1].[PhoneNumberConfirmed] AS [PhoneNumberConfirmed], 
    [Extent1].[TwoFactorEnabled] AS [TwoFactorEnabled], 
    [Extent1].[LockoutEndDateUtc] AS [LockoutEndDateUtc], 
    [Extent1].[LockoutEnabled] AS [LockoutEnabled], 
    [Extent1].[AccessFailedCount] AS [AccessFailedCount], 
    [Extent1].[UserName] AS [UserName]
    FROM [dbo].[AspNetUsers] AS [Extent1]

我通过将 UserId 添加到 AspNetUsers table 来调和程序,然后发生了两件事。

  1. 当我将其添加为列时,我遇到了同样的错误
  2. 当我将主键Id重命名为UserId并做了一个普通的字段ID。 然后我得到的错误是

    InnerException = {"Cannot insert the value NULL into column 'UserId', table 'dbo.AspNetUsers'; column does not allow nulls. INSERT fails.\r\nThe statement has been terminated."}

结论

我从中推断出,在注册时我找不到的某个地方,程序正试图将新用户的 GUID 分配给 Id,但是,数据库正试图保存它作为 UserId

如果我能得到更多帮助来解决这个问题,我将不胜感激。

编辑

// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit https://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        return userIdentity;
    }
}

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<ApplicationUser>()
    //            .Property(p => p.Id)
    //            .HasColumnName("UserId");
    //}
}

这是我最终解决这个问题的方法, 首先,我恢复了我的数据库并做了一个新模型。

然后我在 nugget 包管理器中更新了我的身份模型。 然后我 运行 该应用程序,它给了我一堆关于缺少列的错误。 关键点,上次我尝试这样做时,我在 SQL 中手动创建了列,然后更新了我的模式,这可能会也可能不会一直是我的垮台。

关注这个linkhttps://devblogs.microsoft.com/aspnet/updating-asp-net-applications-from-asp-net-identity-1-0-to-2-0-0-alpha1/

我 运行 启用数据库命令,以及生成 sql 查询的更新命令。

在我的例子中,生成的 SQL 是出于某种原因....错误的,它基本上创建了我已经拥有的表并且没有解决缺失的列,所以 运行 它当然报错了。

使用此 link Migrating to Asp.Net Identity 2.0: new columns not being created in AspNetUsers table 和 Chris Searles 的回答,我将 UP() 和 Down() 函数中的代码更改为他的,这就是解决我的问题的原因。

我的问题可能对我来说是独一无二的,但希望它对某人有所帮助。