关键组件“Id”不是类型上声明的属性

本文关键字:类型 声明 属性 组件 Id | 更新日期: 2023-09-27 18:19:00

我正在使用开箱即用的ApplicationUser,但有一些额外的属性:

public class ApplicationUser : IdentityUser, IPayCaddyEntity
{
    ...
    // TODO Figure out how to config.
    public string FullName { get; set; }
    public string CellNumber { get; set; }
}

然后对于"映射",我将以下基类用于几乎所有实体的EntityConfig类:

abstract class EntityConfigBase<TEntity>: EntityTypeConfiguration<TEntity> where TEntity: class, IPayCaddyEntity
{
    protected EntityConfigBase()
    {
        HasKey(e => e.Id);
    }
}

如果IPayCaddyEntity仅确保所有实体的Id属性:

public interface IPayCaddyEntity
{
    string Id { get; set; }
}

现在,更接近手头的问题,ApplicationUser的配置:

class ApplicationUserConfig: EntityConfigBase<ApplicationUser>
{
    public ApplicationUserConfig()
    {
        Property(e => e.FullName).IsRequired().HasMaxLength(MaxNameLength);
        Property(e => e.CellNumber).IsRequired().HasMaxLength(20);
    }
}

我还使用开箱即用的IdentityDbContext,以及其他应用程序表:

public class PayCaddyDbContext : IdentityDbContext<ApplicationUser>
{
    ...
    public DbSet<AccountPayment> AccountPayments { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        //modelBuilder.Configurations.Add(new ApplicationUserConfig());
        modelBuilder.Configurations.Add(new AccountPaymentConfig());
        base.OnModelCreating(modelBuilder);
    }
}

添加ApplicationUserConfig时,一切都很好,但是当我尝试登录应用程序时,即当它尝试访问用户表时,我收到以下错误:

关键组件"Id"不是类型上的声明属性 "应用程序用户">

在我自己的代码之前调用base.OnModelCreating(modelBuilder)没有明显的区别。

现在它是IdentityUser<TKey, TLogin, TRole, TClaim>上宣布的财产,ApplicationUser最终从中衍生出来。通常继承不是问题,因为我的所有其他实体也从公共基类获取其Id属性:

public abstract class BaseEntity : IBaseEntity
{
    public string Id { get; set; }
}
public class AccountPayment : BaseEntity
{
    ...
}

我对任何其他实体都没有错误(为了简洁起见,我在这里删除了很多(,我能做的最接近的猜测是ApplicationUser仅从接口IBaseEntity派生,而不是从通常提供Id属性的BaseEntity派生。

这是问题的原因,还是是什么,除了直接在OnModelCreating中配置我自己的ApplicationUser属性之外,我是否可以做任何事情来修复它?

关键组件“Id”不是类型上声明的属性

还必须覆盖ApplicationUser实体中的 Id

public override string Id { get; set; }

我认为这是因为实体框架的工作方式无法到达位于基础public virtual TKey Id { get; set; } IdentityUser

您如何实现IPayCaddyEntity接口?我一步一步地重现了您的问题,我没有收到任何错误。第一种方法应该可以正常工作。

public class ApplicationUser : IdentityUser, IPayCaddyEntity
{
    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 string FullName { get; set; }
    public string CellNumber { get; set; }
    //from the IPayCaddyEntity
    public string Id { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Configurations.Add(new ApplicationUserConfig());                
    base.OnModelCreating(modelBuilder);
}