c# - c#身份无法创建自定义属性
问题描述
这是我从 IdentityUser 派生的类:
public class User : IdentityUser
{
public string Extension { get; set; }
}
这是 DbContext
public class SecurityDbContext : IdentityDbContext<User>
{
private string connectionString;
private string dbProvider;
public SecurityDbContext(string connectionString, string dbProvider)
{
this.connectionString = connectionString;
this.dbProvider = dbProvider;
}
在 Startup.cs
services.AddDbContext<SecurityDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("dataContext")));
services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<SecurityDbContext>()
.AddSignInManager<SignInManager<User>>()
.AddDefaultTokenProviders();
我添加了属性扩展,删除了所有表并调用
EnsureDatabasesCreated();
它创建了所有表,但 AspNetUsers 表不包含 Extension 属性。我究竟做错了什么?
解决方案
您还必须使您的上下文继承自IdentityDbContext<TUser>
您TUser
的自定义用户类型(User
在您提供的代码中)也继承自的位置IdentityUser
,例如:
public class ApplicationDbContext : IdentityDbContext<User>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
这是一个netcore webapp吗?如果是这样,请确保您还在您的身份上注册了身份ServiceProvider
并指定了您的自定义用户类,ConfigureServices
方法是:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddSignInManager<SignInManager<User>>()
.AddUserManager<UserManager<User>>();
...
}
如果需要,指定您的自定义User
类和自定义类。Role
推荐阅读
- laravel - 定义模型关系时如何在表间而不是关系表上添加条件
- java - 如何更改浮点转换的精度?
- java - java - 如何在Java中使用OpenPDF创建一个使用动态行跨度/列跨度并放入单元格的表格?
- c# - 在字符串中查找单词并替换它
- azure - 无法在服务结构集群中下载 docker 映像
- android - webview 应用程序:选择文件不起作用
- google-apps-script - 如何从插件加载 Google Apps 脚本 UDF 以在 Google 表格中使用?
- javascript - 如何使用 Node.js 在 Google Cloud Storage 中更改文件的元数据
- jenkins - kubectl set image 抛出错误:服务器没有资源类型部署”
- ffmpeg - 正确清洁 AVFrame