c# - await UserManager.CreateAsync 查找不存在的列

标签 c# asp.net asp.net-mvc entity-framework asp.net-identity

情况

我有一个我更新的工作程序需要更新身份/登录方法。所以我从 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); 行上得到了一个无效的列名'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 表来调和程序,然后发生了两件事。

  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 中手动创建了列,然后更新了我的模式,这可能是也可能不是我的失败。

点击此链接 https://devblogs.microsoft.com/aspnet/updating-asp-net-applications-from-asp-net-identity-1-0-to-2-0-0-alpha1/

我运行了启用数据库命令和更新命令来生成 sql 查询。

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

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

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

关于c# - await UserManager.CreateAsync 查找不存在的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58956235/

相关文章:

c# - 使用 SDK 和 Uploadify 将文件上传到 Amazon S3 时显示的上传进度不正确

c# - 如何让 Crystal Reports 显示项目符号列表

c# - 如何杀死一个窗口?

html - mvc 5 @html.editorfor 类不适合父宽度

.net - 我应该编写自己的 GridView 实现吗?

c# - Web 自定义控件数据存储位置

javascript - 带有 Ajax 按钮的 Asp.net MVC 从数据库中检索数据

c# - Bing Ads API C# 示例代码不起作用

c# - 处理横切问题,例如 Web 应用程序组件的内部统计报告

c# - 如何从javascript读取C#方法输出?