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
我在尝试登录时遇到以下错误
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();
}
}
我查看了以下 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
尝试将 public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>
更改为 public partial class ApplicationDbContext : IdentityDbContext<IdentityUser>
编译器将使用提供给通用 IdentityDbContext<TUser>
class.
的类型生成 DbSet
来自你的startup.cs改变
services.AddDefaultIdentity<IdentityUser>()
至
services.AddDefaultIdentity<ApplicationUser>()
作为后续行动:为了避免下一个可能的问题,一旦这里修复:您还必须更改 Views\Shared_LoginPartial.cshtml
中的类型
来自
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager
到
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
将 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);
}
我已将我的代码从 ASP.NET Core 2.0 升级到 Core 2.1。我创建了一个新的 Core 2.1 项目并将我的代码移动到新项目中。我提供了我的启动示例和 ApplicationDbContext
我在尝试登录时遇到以下错误
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();
}
}
我查看了以下 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
尝试将 public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>
更改为 public partial class ApplicationDbContext : IdentityDbContext<IdentityUser>
编译器将使用提供给通用 IdentityDbContext<TUser>
class.
来自你的startup.cs改变
services.AddDefaultIdentity<IdentityUser>()
至
services.AddDefaultIdentity<ApplicationUser>()
作为后续行动:为了避免下一个可能的问题,一旦这里修复:您还必须更改 Views\Shared_LoginPartial.cshtml
来自
@inject SignInManager<IdentityUser> SignInManager
@inject UserManager<IdentityUser> UserManager
到
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
将 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);
}