ApplicationUser 不能用作泛型类型或方法“ApiAuthorizationDbContext<TUser>”中的类型参数“TUser”

问题描述 投票:0回答:2

在 ASP.NET Core-6 Web API 中,我正在尝试实现 IdentityDBContext。

我有这些型号:

public class ApplicationUser : IdentityUser<long>
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MobileNumber { get; set; }

    [DefaultValue(false)]
    public bool? IsAdmin { get; set; }
    public ICollection<ApplicationUserRole> UserRoles { get; set; }
}

public class ApplicationRole : IdentityRole<long>
{
    public ICollection<ApplicationUserRole> UserRoles { get; set; }
}

public class ApplicationUserRole : IdentityUserRole<long>
{
    public virtual ApplicationUser User { get; set; }
    public virtual ApplicationRole Role { get; set; }
}

我分离了身份和应用程序数据库上下文的上下文:

对于我的身份:

public class ApplicationIdentityDbContext : ApiAuthorizationDbContext<ApplicationUser>
{
    public ApplicationIdentityDbContext(DbContextOptions<ApplicationIdentityDbContext> options,
        IOptions<OperationalStoreOptions> operationalStoreOptions) : base(options, operationalStoreOptions)
    {
    }
}

那么对于申请,我有:

然后在 ApplicationDbContext 中,我有这个:

public interface IApplicationDbContext
{
    public DbSet<RefreshToken> RefreshTokens { get; set; }
}

我将身份键从字符串自定义为长。

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, ApplicationRole, long, IdentityUserClaim<long>, ApplicationUserRole, IdentityUserLogin<long>, IdentityRoleClaim<long>, IdentityUserToken<long>>, IApplicationDbContext
{
    public DbSet<RefreshToken> RefreshTokens { get; set; }

    public ApplicationDbContext(DbContextOptions<DDMDbContext> options)
        : base(options)
    {
    }
}

我在 ApplicationIdentityDbContext 中收到此错误:

错误 CS0311 ApplicationUser 无法用作通用类型或方法“ApiAuthorizationDbContext”中的类型参数“TUser”。没有从“ApplicationUser”到“Microsoft.AspNetCore.Identity.IdentityUser”的隐式引用转换

如何纠正?

谢谢你。

c# asp.net-web-api asp.net-identity
2个回答
0
投票

我认为你应该将 IdentityUserRole 定义为 IdentityUser 和身份角色。 我的意思是为 ApplicationUser 和角色定义 ApplicationUserRole 将您的课程更改为类似:

  public class ApplicationUser : IdentityUser<long , IdentityUserLogin,
ApplicationUserRole ,IdentityUserClaim>
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string MobileNumber { get; set; }
    
        [DefaultValue(false)]
        public bool? IsAdmin { get; set; }
        public ICollection<ApplicationUserRole> UserRoles { get; set; }
    }
    
    public class ApplicationRole : IdentityRole<long,ApplicationUserRole >
    {
        public ICollection<ApplicationUserRole> UserRoles { get; set; }
    }
    
    public class ApplicationUserRole : IdentityUserRole<long>
    {
        public virtual ApplicationUser User { get; set; }
        public virtual ApplicationRole Role { get; set; }
    }

0
投票

安装参考资料:Microsoft.AspNet.Identity.EntityFramework

© www.soinside.com 2019 - 2024. All rights reserved.