ASP.NET Core 2.1 - IdentityUser问题 - 无法为"IdentityUser"创建DbSet此类型未包含在上下文的模型中

Les*_*s P 8 c# asp.net-core-2.1

我已将代码从ASP.NET Core 2.0升级到Core 2.1.我创建了一个新的Core 2.1项目,并将我的代码移到了新项目中.我提供了我的启动和样本ApplicationDbContext

尝试登录时出现以下错误

无法为"IdentityUser"创建DbSet,因为此类型未包含在上下文的模型中.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();
Run Code Online (Sandbox Code Playgroud)

ApplicationDbContext.cs

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

我查看了以下Microsoft文章: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

Mkr*_*yan 19

尝试public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>改为public partial class ApplicationDbContext : IdentityDbContext<IdentityUser>

编译器将使用提供给泛型类的类型生成DbSetIdentityDbContext<TUser>.


小智 6

从您的startup.cs更改

services.AddDefaultIdentity<IdentityUser>()
Run Code Online (Sandbox Code Playgroud)

services.AddDefaultIdentity<ApplicationUser>()
Run Code Online (Sandbox Code Playgroud)


t2t*_*t2t 6

作为跟进:为了避免下一个可能的问题,一旦这里得到修复:您还必须更改 Views\Shared_LoginPartial.cshtml

@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager
Run Code Online (Sandbox Code Playgroud)

@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
Run Code Online (Sandbox Code Playgroud)