【问题标题】:ASP.Net Core - How to add a new field to the default Microsoft Identity structureASP.Net Core - 如何向默认 Microsoft 标识结构添加新字段
【发布时间】:2018-11-26 20:50:24
【问题描述】:

使用 ASP.Net Core 2.1,我试图向默认的 Microsoft Identity Users 表结构添加一个新字段,因此我创建了一个新模型,用于在项目数据库上下文中继承 Microsoft.AspNetCore.Identity,我曾经成功实现过下面的代码,但这次出错了。

这是模型:

public class ApplicationUser : IdentityUser
{
    public string Age { get; set; }
}

这是数据库上下文:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser, string>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { }
    public string CurrentUserId { get; set; }
}

这是输出错误:

非泛型类型“IdentityDbContext”不能与类型参数一起使用

我在这里做错了什么?

【问题讨论】:

    标签: c# asp.net-core asp.net-identity


    【解决方案1】:

    我不确定,但我没有看到接受这 2 个类型参数的基本构造函数。

    MSDN

    有效的类型参数是:

    //none
    public class IdentityDbContext
    

    //application user
    public class IdentityDbContext<TUser> 
    

    //Thanks to @ Ivvan
    public class IdentityDbContext<TUser,TRole,TKey>
    

    //the full version
    public class IdentityDbContext<TUser, TRole, TKey, TUserLogin, TUserRole, TUserClaim>
    

    所以在你的情况下,我认为你的意思是:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    

    【讨论】:

    • EF6 是这样,但 EF Core 中有 extra constructor,它接受 3 个参数:TUser、TRole、TKey。但无论如何,OP 漏掉了一个参数。
    • @Ivvan:感谢提醒...我迷失在 Forrest of docs 中。
    • @Ivvan,仅供参考,以前我使用它有 3 个参数:TUser、TRole、TKey,目前我只需要一个参数 TUser,现在在我删除丢失的 Tkey 后它可以完美运行(字符串)。
    • @Stefan,是的,你是对的,这只是一个愚蠢的错误,我复制了带有 3 个参数的代码并且错过了删除第三个参数:“字符串”,因为我不需要我项目中的角色,现在可以正常使用: public class ApplicationDbContext : IdentityDbContext
    猜你喜欢
    • 2022-07-18
    • 2018-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多