ASP.NET Core 2.1-IdentityUser问题-无法为'IdentityUser'创建DbSet此类型不包含在上下文模型中 [英] ASP.NET Core 2.1 - IdentityUser Issue - Cannot create a DbSet for 'IdentityUser' this type is not included in the model for the context
问题描述
我已经将代码从ASP.NET Core 2.0升级到了Core 2.1.我创建了一个新的Core 2.1项目,并将代码移到了新项目中.我提供了我的创业公司和ApplicationDbContext
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
无法为"IdentityUser"创建DbSet,因为此类型不是 包含在上下文模型中. Microsoft.EntityFrameworkCore.Internal.InternalDbSet.get_EntityType()
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
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
ApplicationDbContext.cs
public partial class ApplicationDbContext :
IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext>
options)
: base(options)
{
Database.EnsureCreated();
}
}
我已经阅读了以下Microsoft文章: https://blogs.msdn.microsoft.com/webdev/2018/05/30/asp-net-core-2-1-0-now-available/
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
推荐答案
尝试将public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>
更改为public partial class ApplicationDbContext : IdentityDbContext<IdentityUser>
编译器将使用以下命令生成 DbSet 通用IdentityDbContext<TUser>
类提供的类型.
Compiler will generate DbSet with the type provided to generic IdentityDbContext<TUser>
class.
这篇关于ASP.NET Core 2.1-IdentityUser问题-无法为'IdentityUser'创建DbSet此类型不包含在上下文模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!