首页 > 解决方案 > ASP.Net Core - 如何向默认 Microsoft 标识结构添加新字段

问题描述

使用 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-coreasp.net-identity

解决方案


我不确定,但我没有看到接受这 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>

推荐阅读