简体   繁体   English

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

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.

相关问题 无法为“ IdentityUser”创建DbSet,因为此类型不包含在上下文模型中 - Cannot create a DbSet for 'IdentityUser' because this type is not included in the model for the context 在ASP.NET Core 2.1中更改IdentityUser类型 - Changing IdentityUser Type in ASP.NET Core 2.1 ASP.NET Core(2.1)中的身份:为什么我们需要添加新的Context来自定义IdentityUser? - Identity in ASP.NET Core (2.1): Why do we need to add a new Context to customize IdentityUser? 更改asp.net core 2.2 IdentityUser的ID类型 - Change Id type of asp.net core 2.2 IdentityUser 实体类型IdentityUser不是ASP.NET Identity中当前上下文的模型的一部分 - The entity type IdentityUser is not part of the model for the current context in ASP.NET Identity .Net核心单元测试错误:无法为“ IdentityRole”创建DbSet,因为此类型不包含在上下文模型中 - .Net Core Unit Tests error: Cannot create a DbSet for 'IdentityRole' because this type is not included in the model for the context Asp.Net Core Identity、IdentityUser.Email 与 UserManage<identityuser> .GetEmailAsync(IdentityUser 用户)</identityuser> - Asp.Net Core Identity, IdentityUser.Email versus UserManage<IdentityUser>.GetEmailAsync(IdentityUser user) ASP.NET CORE 2 IdentityUser POCO 错误 - ASP.NET CORE 2 IdentityUser POCO Error 无法从ASP.NET Core 2.1控制器获取IdentityUser数据 - Unable to get IdentityUser data from ASP.NET Core 2.1 controller 具有自定义返回类型的 Entity Framework Core - 无法为“BookInfo”创建 DbSet,因为此类型未包含在上下文模型中 - Entity Framework Core with Custom Return Type - Cannot create a DbSet for 'BookInfo' because this type is not included in the model for the context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM