简体   繁体   中英

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

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();
    }
}

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

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.

我遇到了这个问题,因为我在 OnModelCreating 方法的 DbContext 中做了一些涉及 ApplicationUser 的关系更改,并且我没有更新我的迁移。

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