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

查看:36
本文介绍了ASP.NET Core 2.1 - IdentityUser 问题 - 无法为“IdentityUser"创建 DbSet 此类型未包含在上下文模型中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已将代码从 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

//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 改为 public partial class ApplicationDbContext : IdentityDbContext代码>

Try changing public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser> to public partial class ApplicationDbContext : IdentityDbContext<IdentityUser>

编译器将生成 DbSet类型提供给通用 IdentityDbContext 类.

Compiler will generate DbSet with the type provided to generic IdentityDbContext<TUser> class.

这篇关于ASP.NET Core 2.1 - IdentityUser 问题 - 无法为“IdentityUser"创建 DbSet 此类型未包含在上下文模型中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆