简体   繁体   English

等待 UserManager.CreateAsync 寻找不存在的列

[英]await UserManager.CreateAsync looking for column that does not exist

Situation情况

I had a working program which I updated needed to update the identity/login in method.我有一个我更新的工作程序需要更新身份/登录方法。 So I updated from Identity 1 to version2 and I have been fixing errors as they come but I am currently stumped.所以我从 Identity 1 更新到 version2 并且我一直在修复错误,但我目前很难过。

This is my Registration Code这是我的注册码

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)

Problem问题

The problem which is occurring is I am getting an Invalid column name 'UserId' on line var result = await UserManager.CreateAsync(user, model.Password);正在发生的问题是我在var result = await UserManager.CreateAsync(user, model.Password); Invalid column name 'UserId' . .
I am not entirely sure why this is occurring (or where this UserId is coming from, maybe from the old identity?) because when I place a breakpoint on the line the SQL shows the query as我不完全确定为什么会发生这种情况(或者这个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]

I humored the program by adding UserId to the AspNetUsers table and two things happened.我通过将UserId添加到 AspNetUsers 表来使程序变得幽默,并且发生了两件事。

  1. When I added it as a column I got the same error当我将它添加为列时,我得到了同样的错误
  2. When I renamed the primary key Id to UserId and made a normal field ID.当我将主键Id重命名为UserId并制作了一个普通的字段 ID。 Then the error I got was然后我得到的错误是

    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."} InnerException = {"无法将值 NULL 插入到列 'UserId',表 'dbo.AspNetUsers';列不允许空值。INSERT 失败。\r\n语句已终止。"}

Conclusion结论

What I am deducing from this is, somewhere that I cant find, on Registration, the program is trying to assign the GUID of a new user to Id , However, the database is trying to save it as UserId .我从中推断的是,在我找不到的地方,在注册时,程序正试图将新用户的 GUID 分配给Id ,但是,数据库正试图将其保存为UserId

Would appreciate if I could get more assistance in solving this.如果我能在解决这个问题上获得更多帮助,我将不胜感激。

Edit编辑

// 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");
    //}
}

Here's how I finally solved this, First off, I reverted my database and did a new model.这是我最终解决这个问题的方法,首先,我恢复了我的数据库并做了一个新的 model。

I then updated my Identity Model in nugget package manager.然后我在 nugget package 管理器中更新了我的身份 Model。 I then ran the app and it gave me a bunch of errors about missing columns.然后我运行了这个应用程序,它给了我一堆关于缺少列的错误。 Crucial point , the last time I attempted this I created the columns manually in SQL then updated my mode, this may or may not have been my downfall.关键点,上次我尝试这样做时,我在 SQL 中手动创建了列,然后更新了我的模式,这可能是也可能不是我的失败。

Following This link https://devblogs.microsoft.com/aspnet/updating-asp-net-applications-from-asp-net-identity-1-0-to-2-0-0-alpha1/按照此链接https://devblogs.microsoft.com/aspnet/updating-asp-net-applications-from-asp-net-identity-1-0-to-2-0-0-alpha1/

I ran the Enable database command,and the update command to generate the sql queries.我运行了启用数据库命令和更新命令来生成 sql 查询。

In my case, the SQL that was generated was for some reason....wrong, it basically created tables I already had and did not address the missing columns, so running it of course gave an error.就我而言,生成的 SQL 出于某种原因......错误,它基本上创建了我已经拥有的表并且没有解决缺失的列,因此运行它当然会出错。

Using this link Migrating to Asp.Net Identity 2.0: new columns not being created in AspNetUsers table and the answer from Chris Searles, I changed the code that was in the UP() and Down() function to his and that is what fixed my problem.使用此链接Migrating to Asp.Net Identity 2.0: new columns not being created in AspNetUsers table和 Chris Searles 的答案,我将 UP() 和 Down() function 中的代码更改为他的,这就是修复我的问题。

My issue might be unique to me but, hope it helps someone.我的问题可能对我来说是独一无二的,但希望它对某人有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UserManager.CreateAsync 不生成 Id - UserManager.CreateAsync does not generate Id UserManager.CreateAsync不会在ApplicationUser中保存添加的属性 - UserManager.CreateAsync does not save added properties in ApplicationUser ASP.Net.Core userManager.CreateAsync - ASP.Net.Core userManager.CreateAsync 名称不能为null或为空:_userManager.CreateAsync - Name cannot be null or empty: _userManager.CreateAsync UserManager.CreateAsync。成功总是返回false - UserManager.CreateAsync .Success always returns false 正在使用的错误案例列表_userManager.CreateAsync(user,password) - List of error cases in use _userManager.CreateAsync(user, password) UserManager.CreateAsync(用户,密码)陷入无限循环 - UserManager.CreateAsync(user, password) stuck in infinite loop Foo在UserManager.CreateAsync上的关系栏错误上的角色冲突更改 - Conflicting changes to the role of Foo on relationship Bar error on UserManager.CreateAsync 如何删除使用UserManager.CreateAsync创建的用户 - How to delete users that were created with UserManager.CreateAsync Asp .Net Core 单元测试模拟 UserManager.CreateAsync 并返回标识结果 - Asp .Net Core Unit Test Mock UserManager.CreateAsync and return Identity Result
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM