我如何使用 ASP.NET Core identity v preview 3.0 创建自定义用户和角色?
How i can create custom user and role with ASP.NET Core identity v preview 3.0?
创建迁移时出现错误,如下所示:
System.InvalidOperationException: Cannot create a DbSet for 'IdentityRole' because this type is not included in the model for the context.
System.InvalidOperationException: Cannot create a DbSet for 'UserApp' because this type is not included in the model for the context.
是的,我的上下文中没有此列,但在以前的版本中,没有它也能正常工作。
我的代码是:
public class UserApp: IdentityUser
{
[PersonalData]
public int Year { get; set; }
[PersonalData]
public string Country { get; set; }
public List<Product> products { get; set; }
}
和上下文 class:
public class ApplicationContext:DbContext
{
public ApplicationContext()
{
}
//public ApplicationContext(DbContextOptions options) : base(options) { }
public ApplicationContext(DbContextOptions<ApplicationContext> dbContext) : base(dbContext)
{
}
有一些数据库集。在启动 class 中,我有:
services.AddIdentity<UserApp, IdentityRole>(o =>
{
o.Password.RequireDigit = false;
o.Password.RequireLowercase = false;
o.Password.RequireUppercase = false;
o.Password.RequireNonAlphanumeric = false;
o.Password.RequiredLength = 6;
})
.AddEntityFrameworkStores<ApplicationContext>()
.AddDefaultTokenProviders();
怎么了?实际上,在这个版本中,我必须将用户和角色的 属性?
添加到我的上下文中
问题是您的 ApplicationContext
继承了 DbContext
而不是 IdentityDbContext
。所以你的 ApplicationContext
应该是这样的:
public class ApplicationContext : IdentityDbContext<UserApp, IdentityRole, string>
{
public ApplicationContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
创建迁移时出现错误,如下所示:
System.InvalidOperationException: Cannot create a DbSet for 'IdentityRole' because this type is not included in the model for the context.
System.InvalidOperationException: Cannot create a DbSet for 'UserApp' because this type is not included in the model for the context.
是的,我的上下文中没有此列,但在以前的版本中,没有它也能正常工作。 我的代码是:
public class UserApp: IdentityUser
{
[PersonalData]
public int Year { get; set; }
[PersonalData]
public string Country { get; set; }
public List<Product> products { get; set; }
}
和上下文 class:
public class ApplicationContext:DbContext
{
public ApplicationContext()
{
}
//public ApplicationContext(DbContextOptions options) : base(options) { }
public ApplicationContext(DbContextOptions<ApplicationContext> dbContext) : base(dbContext)
{
}
有一些数据库集。在启动 class 中,我有:
services.AddIdentity<UserApp, IdentityRole>(o =>
{
o.Password.RequireDigit = false;
o.Password.RequireLowercase = false;
o.Password.RequireUppercase = false;
o.Password.RequireNonAlphanumeric = false;
o.Password.RequiredLength = 6;
})
.AddEntityFrameworkStores<ApplicationContext>()
.AddDefaultTokenProviders();
怎么了?实际上,在这个版本中,我必须将用户和角色的 属性?
添加到我的上下文中问题是您的 ApplicationContext
继承了 DbContext
而不是 IdentityDbContext
。所以你的 ApplicationContext
应该是这样的:
public class ApplicationContext : IdentityDbContext<UserApp, IdentityRole, string>
{
public ApplicationContext(DbContextOptions<ApplicationDbContext> options)
: base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}