ASP.NET Core 2.1 - IdentityUser Issue - Cannot create a DbSet for 'IdentityUser' this type is not included in the model for the context

Lek Pio picture Lek Pio · Jun 7, 2018 · Viewed 18k times · Source

I have upgraded my code from ASP.NET Core 2.0 to Core 2.1. I created a new Core 2.1 project and moved my code into the new project. I have provided samples of my startup and ApplicationDbContext

I get the following error when trying to Log In

Cannot create a DbSet for 'IdentityUser' because this type is not included in the model for the context. Microsoft.EntityFrameworkCore.Internal.InternalDbSet.get_EntityType()

startup.cs

//Core 2.1
  services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultTokenProviders();            

////Old Core 2.0 Code
  //services.AddIdentity<ApplicationUser, IdentityRole>()
        //    .AddEntityFrameworkStores<ApplicationDbContext>()
        //    .AddDefaultTokenProviders();

ApplicationDbContext.cs

public partial class ApplicationDbContext : 
    IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> 
 options)
        : base(options)
    {
        Database.EnsureCreated();
     }
 }

I have reviewed the following Microsoft articles : https://blogs.msdn.microsoft.com/webdev/2018/05/30/asp-net-core-2-1-0-now-available/

https://docs.microsoft.com/en-us/aspnet/core/migration/20_21?view=aspnetcore-2.1

Answer

Mkrtich Mazmanyan picture Mkrtich Mazmanyan · Jun 7, 2018

Try changing public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser> to public partial class ApplicationDbContext : IdentityDbContext<IdentityUser>

Compiler will generate DbSet with the type provided to generic IdentityDbContext<TUser> class.