本文介绍了错误:“无效的列名'ApplicationUser_Id'”;添加索赔时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像这样在存储库中添加声明,并且运行良好:

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

截屏:

在第一个AddOrUpdate的UserClaims存储库中引发了错误:

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

感谢您的帮助!

编辑:默认实体根本没有列。

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

这里是ApplicationUser实体

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


推荐答案

此发生错误是因为 ApplicationUser IdentityUser 继承而Entity Framework无法找出正确的ID,因此无法找到正确的 Id中将其写为 ApplicationUser_Id。

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

要更正此错误,您可以将以下行添加到ApplicationDbContext.OnModelCreating(DbModelBuilder modelBuilder):

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

这篇关于错误:“无效的列名'ApplicationUser_Id'”;添加索赔时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 13:04