L. Piotrowski
L. Piotrowski

Reputation: 1589

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://learn.microsoft.com/en-us/aspnet/core/migration/20_21?view=aspnetcore-2.1

Upvotes: 26

Views: 38363

Answers (4)

t2t
t2t

Reputation: 1251

As a follow up: to avoid the next possible issue as soon as this here is fixed: You also have to change the types in the Views\Shared_LoginPartial.cshtml

From

@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager

To

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager

Upvotes: 7

AliRam
AliRam

Reputation: 80

Change the ApplicationDbContext to:

private static bool _Created = false;

public ApplicationDbContext()
{
    if (!_Created)
    {
        _Created = true;
        Database.EnsureCreated();

    }
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder.UseSqlServer(@"server = .\SQLSERVER; initial catalog = DBName; Integrated Security = True; MultipleActiveResultSets = True; App = EntityFramework & quot; ");
}

protected override void OnModelCreating(ModelBuilder builder)
{
    base.OnModelCreating(builder);
}

Upvotes: -1

Chpn Dave
Chpn Dave

Reputation: 1570

From Your startup.cs change

services.AddDefaultIdentity<IdentityUser>()

To

services.AddDefaultIdentity<ApplicationUser>()

Upvotes: 26

Mike
Mike

Reputation: 4051

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.

Upvotes: 44

Related Questions