Cannot create a dbset for ‘identityrole’ because this type is not included in the model for the context.

When you encounter the error message “cannot create a DbSet for ‘IdentityRole’ because this type is not included in the model for the context”, it means that the ‘IdentityRole’ class is not configured to be included in the database context.

To fix this issue, you need to ensure that the ‘IdentityRole’ class is added to the database context along with other entity classes. Here’s an example to demonstrate how to include ‘IdentityRole’ in the context:


public class ApplicationDbContext : IdentityDbContext
{
    public ApplicationDbContext(DbContextOptions options)
        : base(options)
    {
    }

    public DbSet Roles { get; set; } // Include IdentityRole in the context

    // Add other entity sets here
    // public DbSet<YourEntity> YourEntities { get; set; }
}
   

In the above example, we have created a custom ‘ApplicationDbContext’ class that inherits from ‘IdentityDbContext‘ provided by the Identity framework. We then added DbSet property to the context so that it includes ‘IdentityRole’ in the context.

Make sure to add other entity sets (DbSet) for your application’s entities as well, as shown in the code comment “// Add other entity sets here”.

Once you have added ‘IdentityRole’ and other entities to the context, you should be able to work with them using the database context in your application.

Same cateogry post

Leave a comment