22

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

awh112
  • 1,466
  • 4
  • 22
  • 34
L. Piotrowski
  • 1,533
  • 1
  • 20
  • 35

4 Answers4

36

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.

Mike
  • 3,766
  • 3
  • 18
  • 32
20

From Your startup.cs change

services.AddDefaultIdentity<IdentityUser>()

To

services.AddDefaultIdentity<ApplicationUser>()
Chpn Dave
  • 1,350
  • 2
  • 9
  • 5
6

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
t2t
  • 1,101
  • 2
  • 12
  • 15
-1

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);
}
codevision
  • 5,165
  • 38
  • 50
AliRam
  • 60
  • 1
  • 8