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

65,091

Solution 1

Added this and it worked:

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

Solution 2

Check that your AppDbContext is NOT inherited from DbContext but instead it should be inherited from IdentityDbContext<ApplicationUser>

Solution 3

The most common reasons for

Cannot create a DbSet for 'THE-MODEL' because this type is not included in the model for the context

are as follows

  1. The model name does not match with the table name in database
  2. EntityFramework cannot figure out required meta by convention and you have not overriden it.

in your case Role inherits IdentityRoleClaim and that was not configured and default convention required "Id" as key but i suppose it did not have that property so it had to be configured. It would also have worked if you created property in Role like Id => new{UserId,RoleId} which would by convention present Id as the key property to entity framework.

Solution 4

You can fix the issue by replacing this

public class ApplicationDbContext : IdentityDbContext<AppUser>

with this

public class ApplicationDbContext : IdentityDbContext<AppUser, AppRole, string>

notice this string type in the last argument.

Solution 5

If your DbContext doesn't inherit IdentityUserContext - don't use AddEntityFrameworkStores in the ConfigureServices method. Substitute it with AddUserStore and AddRoleStore as follows:

public void ConfigureServices(IServiceCollection services)
{
    ...
    services
        .AddIdentity<MyUserType, MyRoleType>(...)
        .AddUserStore<UserStore<MyUserType, MyRoleType, MyDbContextType, MyKeyType, MyUserClaimType, MyUserRoleType, MyUserLoginType, MyUserTokenType, MyRoleClaimType>>()
        .AddRoleStore<RoleStore<MyRoleType, MyDbContextType, MyKeyType, MyUserRoleType, MyRoleClaimType>>();
    ...
}

If you check the implementation of the AddEntityFrameworkStores method you'll see that it adds stores via searching for generic types in the DbContext, assuming that it inherits IdentityUserContext. Regardless, you'll be stuck with the base Identity* types for claims ... which will generate this exception.

Share:
65,091
001
Author by

001

Only questions with complete answers are accepted as solutions.

Updated on September 18, 2021

Comments

  • 001
    001 over 2 years

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

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

    What is wrong?


    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>