[英]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:该错误在我的 UserClaims 存储库中的第一个 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这是 ApplicationUser 实体
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."发生此错误是因为
ApplicationUser
继承自IdentityUser
并且实体框架无法找出正确的 ID,因此它写入了“ApplicationUser_Id”而不是正确的“Id”。
To correct this, you can add the following line to ApplicationDbContext.OnModelCreating(DbModelBuilder modelBuilder):要纠正此问题,您可以将以下行添加到 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 -我在使用 asp.net core 3 时遇到了同样的问题,所以我在 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);重要的是首先调用 base.OnModelCreating(modelBuilder); after that assign your table relationship.
之后分配您的表关系。 That was solved my problem.
那解决了我的问题。
我遇到了这个问题,因为我在 OnModelCreating 方法的 DbContext 中做了一些涉及 ApplicationUser 的关系更改,并且我没有更新我的迁移。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.