【问题标题】:How i can create custom user and role with ASP.NET Core identity v preview 3.0?如何使用 ASP.NET Core 标识 v 预览版 3.0 创建自定义用户和角色?
【发布时间】:2019-05-24 14:53:17
【问题描述】:

当我创建迁移时,出现错误,如下所示:

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; }
}

和上下文类:

public class ApplicationContext:DbContext
{
    public ApplicationContext()
    {

    }

    //public ApplicationContext(DbContextOptions options) : base(options) { }
    public ApplicationContext(DbContextOptions<ApplicationContext> dbContext) : base(dbContext)
    {

    } 

带有一些数据库集。在启动课上,我有:

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?那堂课完全是空的。那应该是ApplicationContext : IdentityDbContext...
  • @camilo-terevinto,是的,我忘记了这一点,但在这种情况下没有什么改变。 ://

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


【解决方案1】:

问题是您的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);

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-01
    • 2016-04-25
    • 2012-03-18
    • 1970-01-01
    • 2023-02-21
    • 2016-03-07
    • 2016-03-14
    • 2018-09-03
    相关资源
    最近更新 更多