[英]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.我已将代码从 ASP.NET Core 2.0 升级到 Core 2.1。 I created a new Core 2.1 project and moved my code into the new project.我创建了一个新的 Core 2.1 项目并将我的代码移动到新项目中。 I have provided samples of my startup and ApplicationDbContext
我提供了我的启动和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.无法为“IdentityUser”创建 DbSet,因为此类型未包含在上下文的模型中。 Microsoft.EntityFrameworkCore.Internal.InternalDbSet.get_EntityType() 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应用程序数据库上下文.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/我查看了以下 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 https://docs.microsoft.com/en-us/aspnet/core/migration/20_21?view=aspnetcore-2.1
Try changing public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>
to public partial class ApplicationDbContext : IdentityDbContext<IdentityUser>
尝试将public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>
更改为public partial class ApplicationDbContext : IdentityDbContext<IdentityUser>
Compiler will generate DbSet with the type provided to generic IdentityDbContext<TUser>
class.编译器将使用提供给通用IdentityDbContext<TUser>
类的类型生成DbSet 。
From Your startup.cs change从您的 startup.cs 更改
services.AddDefaultIdentity<IdentityUser>()
To到
services.AddDefaultIdentity<ApplicationUser>()
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
作为跟进:为了避免下一个可能的问题,一旦这里修复:您还必须更改Views\\Shared_LoginPartial.cshtml
的类型
From从
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager
To到
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
Change the ApplicationDbContext
to:将ApplicationDbContext
更改为:
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.