c# 身份无法创建自定义 属性
c# identity can't create custom property
这是我的 class 派生自 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();
我添加了 属性 分机,删除了所有 table 并调用了
EnsureDatabasesCreated();
它创建了所有 table,但 AspNetUsers table 不包含扩展 属性。
我做错了什么?
您还必须使您的上下文继承自 IdentityDbContext<TUser>
,其中 TUser
是您自定义的用户类型(您提供的代码中的 User
)也继承自 IdentityUser
,例如:
public class ApplicationDbContext : IdentityDbContext<User>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
这是 netcore webapp 吗?如果是这样,请确保您还在 ServiceProvider
上注册了身份并在 ConfigureServices
方法中指定了您的自定义用户 class:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddSignInManager<SignInManager<User>>()
.AddUserManager<UserManager<User>>();
...
}
如果需要,指定您的自定义 User
class 和自定义 Role
class。
原来我必须将此字段添加到我的 RegisterRequest。
这是我的 class 派生自 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();
我添加了 属性 分机,删除了所有 table 并调用了
EnsureDatabasesCreated();
它创建了所有 table,但 AspNetUsers table 不包含扩展 属性。 我做错了什么?
您还必须使您的上下文继承自 IdentityDbContext<TUser>
,其中 TUser
是您自定义的用户类型(您提供的代码中的 User
)也继承自 IdentityUser
,例如:
public class ApplicationDbContext : IdentityDbContext<User>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
}
这是 netcore webapp 吗?如果是这样,请确保您还在 ServiceProvider
上注册了身份并在 ConfigureServices
方法中指定了您的自定义用户 class:
public void ConfigureServices(IServiceCollection services)
{
...
services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddSignInManager<SignInManager<User>>()
.AddUserManager<UserManager<User>>();
...
}
如果需要,指定您的自定义 User
class 和自定义 Role
class。
原来我必须将此字段添加到我的 RegisterRequest。