Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 474
L. Piotrowski Avatar asked Jun 07 '18 14:06

L. Piotrowski


2 Answers

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.

like image 54
Mike Avatar answered Oct 05 '22 00:10

Mike


From Your startup.cs change

services.AddDefaultIdentity<IdentityUser>()

To

services.AddDefaultIdentity<ApplicationUser>()
like image 23
Chpn Dave Avatar answered Oct 05 '22 00:10

Chpn Dave