简体   繁体   English

添加声明时出现错误:“无效的列名 'ApplicationUser_Id'”

[英]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.

相关问题 C# Entity Framework 6 MySQL - 在插入 ApplicationUser_Id 时引用了未知列 - C# Entity Framework 6 MySQL - Unknown column being referenced on insert ApplicationUser_Id 无效的列名TableName_ID错误 - Invalid column name TableName_ID error 无效的列名 *Id - Invalid Column Name *Id 实体框架引发错误-无效的列名&#39;Id&#39;。\\ r \\ n无效的列名&#39;Id&#39;。” - Entity Framework throws error -Invalid column name 'Id'.\r\n Invalid column name 'Id'." SQL Server添加字符串值时的列名无效 - SQL Server Invalid column name when adding string value 添加不在数据库表中的属性时,实体框架无效的列名 - Entity Framework Invalid column name when adding property that is not in db table 将数据插入两个表时=&gt;错误:“无效的列名ID” - While Inserting data into two tables =>ERROR: “Invalid column name Id” 实体框架抛出无效的列名User_Id错误 - Entity Framework throws invalid column name User_Id error 扩展MVC5和实体框架6 ApplicationUser导致用户注册无效列错误 - Extending MVC5 and Entity Framework 6 ApplicationUser causing invalid column error on user registration EF映射表错误“无效的列名XXX_Id” - EF Mapping Table Error “Invalid column name XXX_Id”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM