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

℡╲_俬逩灬. 提交于 2019-12-04 03:17:01

问题


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


回答1:


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.




回答2:


From Your startup.cs change

services.AddDefaultIdentity<IdentityUser>()

To

services.AddDefaultIdentity<ApplicationUser>()



回答3:


As a follow up, to avoid the next possible issue, when 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



回答4:


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);
}


来源:https://stackoverflow.com/questions/50744006/asp-net-core-2-1-identityuser-issue-cannot-create-a-dbset-for-identityuser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!