簡體   English   中英

添加聲明時出現錯誤:“無效的列名 'ApplicationUser_Id'”

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

我正在像這樣在存儲庫中添加聲明並且它工作正常:

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

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

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

但是當我將應用程序用戶添加到代碼中時,它會引發下一個代碼塊下方顯示的錯誤:

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

截屏:

在此處輸入圖片說明

該錯誤在我的 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();
}

謝謝你的幫助!

編輯:默認實體根本沒有列。

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 實體

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並且實體框架無法找出正確的 ID,因此它寫入了“ApplicationUser_Id”而不是正確的“Id”。

要糾正此問題,您可以將以下行添加到 ApplicationDbContext.OnModelCreating(DbModelBuilder modelBuilder):

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

我在使用 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();
            });
}

重要的是首先調用 base.OnModelCreating(modelBuilder); 之后分配您的表關系。 那解決了我的問題。

我遇到了這個問題,因為我在 OnModelCreating 方法的 DbContext 中做了一些涉及 ApplicationUser 的關系更改,並且我沒有更新我的遷移。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM