InvalidOperationException:无法为“角色”创建DbSet,因为此类型不包含在上下文模型中 [英] InvalidOperationException: Cannot create a DbSet for 'Role' because this type is not included in the model for the context

查看:1582
本文介绍了InvalidOperationException:无法为“角色”创建DbSet,因为此类型不包含在上下文模型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下解决方案在.net core 1.1中有效,但是从1.1升级到2.0后,我收到以下错误:

The following solution works in .net core 1.1, but after upgrading from 1.1 to 2.0, I received the following error:


InvalidOperationException :无法为角色创建DbSet,因为此类型不包含在上下文模型中。

InvalidOperationException: Cannot create a DbSet for 'Role' because this type is not included in the model for the context.

当用户尝试执行以下操作时:登录并执行以下语句:

When the user attempts to log in and the following statement is executed:

var result = await _signInManager.PasswordSignInAsync(model.Email, 
                                model.Password, model.RememberMe, lockoutOnFailure: false);

出什么问题了?

User.cs

public partial class User : IdentityUser<Guid>
{
    public string Name { get; set; }
}

IdentityEntities.cs

public partial class UserLogin : IdentityUserLogin<Guid>
{
}
public partial class UserRole : IdentityUserRole<Guid>
{
}
public partial class UserClaim : IdentityUserClaim<Guid>
{
}
public partial class Role : IdentityRole<Guid>
{
    public Role() : base()
    { 
    }

    public Role(string roleName)
    {
        Name = roleName;
    }
}
public partial class RoleClaim : IdentityRoleClaim<Guid>
{
}
public partial class UserToken : IdentityUserToken<Guid>
{
}

ConfigureServices

services.AddIdentity<User, Role> 


推荐答案

添加并成功了:

builder.Entity<IdentityUserRole<Guid>>().HasKey(p => new { p.UserId, p.RoleId });

这篇关于InvalidOperationException:无法为“角色”创建DbSet,因为此类型不包含在上下文模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆