Error: “Invalid column name 'ApplicationUser_Id'” when adding claims

拟墨画扇 提交于 2021-01-04 07:43:40

问题


I was adding claims in the repository like this and it was working fine:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // ...

    modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");

    modelBuilder.Configurations.Add(new ExperienceMap());
}

But when I added the application user to the code, it throws the error shown below the next block of code:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // ...

    //Error occurs when I add this
    modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers").HasKey<string>(l => l.Id);

    modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");
    modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
    modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
    modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });

    modelBuilder.Configurations.Add(new ExperienceMap());
}

Screenshot:

The error is thrown in my UserClaims repo at the first AddOrUpdate:

public void InsertOrUpdate(IEnumerable<IdentityUserClaim> claims, Expression<Func<IdentityUserClaim, Object>> identifierExpression = null)
{
    foreach (var claim in claims)
    {
        if (identifierExpression != null)
            DataAccess.UserClaims.AddOrUpdate(identifierExpression, claim); //where the error occurs
        else
            DataAccess.UserClaims.AddOrUpdate(claim);
    }
    DataAccess.SaveChanges();
}

Thanks for your help!

EDIT: The default entity does not have column at all.

namespace Microsoft.AspNet.Identity.EntityFramework
{
    public class IdentityUserClaim<TKey>
    {
        public IdentityUserClaim();
        public virtual string ClaimType { get; set; }
        public virtual string ClaimValue { get; set; }
        public virtual int Id { get; set; }
        public virtual TKey UserId { get; set; }
    }
}

Here is the ApplicationUser Entity

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 bool FirstTimeLogin { get; set; }
    public bool HasConsent { get; set; }
    public DateTime ConsentDate { get; set; }
}

public class ApplicationIdentityContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationIdentityContext()
        : base("ApplicationDbContext", throwIfV1Schema: false)
    {
    }

    public static ApplicationIdentityContext Create()
    {
        return new ApplicationIdentityContext();
    }
}

回答1:


This error occurs because ApplicationUser inherits from IdentityUser and Entity Framework can't figure out the proper ID so instead of the correct "Id" it writes "ApplicationUser_Id."

To correct this, you can add the following line to ApplicationDbContext.OnModelCreating(DbModelBuilder modelBuilder):

modelBuilder.Entity<ApplicationUser>().HasKey<string>(l => l.Id); 



回答2:


I had same problem while i was working with asp.net core 3 so, i did this in DbContext -

protected override void OnModelCreating(ModelBuilder modelBuilder)
{          

   base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<ApplicationUser>(b =>
            {
                //Each User can have many entries in the UserRole join table
                b.HasMany(e => e.Roles)
                    .WithOne()
                    .HasForeignKey(ur => ur.UserId)
                    .IsRequired();
            });
}

Important is first call base.OnModelCreating(modelBuilder); after that assign your table relationship. That was solved my problem.




回答3:


I had this issue because I did some relation changes involving ApplicationUser in the DbContext on the OnModelCreating method and I didn't update my migration.



来源:https://stackoverflow.com/questions/43096084/error-invalid-column-name-applicationuser-id-when-adding-claims

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!