简体   繁体   中英

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.

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); .
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

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.

  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. 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."}

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 .

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.

I then updated my Identity Model in nugget package manager. 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.

Following This link 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.

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.

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.

My issue might be unique to me but, hope it helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM