实体类型 ApplicationUser 不是当前上下文模型的一部分

Posted

技术标签:

【中文标题】实体类型 ApplicationUser 不是当前上下文模型的一部分【英文标题】:The entity type ApplicationUser is not part of the model for the current context 【发布时间】:2014-07-16 14:52:09 【问题描述】:

我正在从身份 1.0.0 迁移到身份 2.0.1,遵循此 article

生成的迁移代码与新的 IdentityUser 无关。它不会添加新列。

所以我创建了一个新项目并再次尝试,但迁移代码为空。

为了解决这个问题,我直接在 SQL Server 中进行了编辑,并在我的解决方案中再次导入了我的数据库。

现在我的AspNetUser 和我的IdentityUser 完全一样,你可以看到

身份用户

public virtual int AccessFailedCount  get; set; 

public virtual ICollection<TClaim> Claims  get; 

public virtual string Email  get; set; 

public virtual bool EmailConfirmed  get; set; 

public virtual TKey Id  get; set; 

public virtual bool LockoutEnabled  get; set; 

public virtual DateTime? LockoutEndDateUtc  get; set; 

public virtual ICollection<TLogin> Logins  get; 

public virtual string PasswordHash  get; set; 

public virtual string PhoneNumber  get; set; 

public virtual bool PhoneNumberConfirmed  get; set; 

public virtual ICollection<TRole> Roles  get; 

public virtual string SecurityStamp  get; set; 

public virtual bool TwoFactorEnabled  get; set; 

public virtual string UserName  get; set; 

IdentityUser.cs

public class ApplicationUser : IdentityUser

    public bool Has_accepted_policy  get; set; 
    public int user_type_id  get; set; 


public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

    public ApplicationDbContext()
        : base("DefaultConnection")
    

    

AspNetUser

public string Id  get; set; 

[Required]
[StringLength(256)]
public string UserName  get; set; 

public string PasswordHash  get; set; 

public string SecurityStamp  get; set; 

[StringLength(256)]
public string Email  get; set; 

public bool EmailConfirmed  get; set; 

public bool Is_Active  get; set; 

[Required]
[StringLength(128)]
public string Discriminator  get; set; 

public int? user_type_id  get; set; 

public bool Has_accepted_policy  get; set; 

public string PhoneNumber  get; set; 

public bool PhoneNumberConfirmed  get; set; 

public bool TwoFactorEnabled  get; set; 

public DateTime? LockoutEndDateUtc  get; set; 

public bool LockoutEnabled  get; set; 

public int AccessFailedCount  get; set; 

... other virtual properties 

当我尝试注册用户时出现以下异常

在这一行

IdentityResult result = await UserManager.CreateAsync(user, model.Password);

我的startup.Auth.cs

UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());

在我的 AccountController 我这样声明我的UserManager

public AccountController()
    : this(Startup.UserManagerFactory(), Startup.OAuthOptions.AccessTokenFormat)



public AccountController(UserManager<ApplicationUser> userManager,
    ISecureDataFormat<AuthenticationTicket> accessTokenFormat)

    UserManager = userManager;
    AccessTokenFormat = accessTokenFormat;


public UserManager<ApplicationUser> UserManager  get; private set; 

除了AspNetUser 类中的新属性外,我没有更改任何内容,并且它在迁移之前运行良好。

CodePlex 上有一个类似的问题标记为已修复,但他们没有给出解决方案

有谁知道如何解决这个问题?

编辑

为了确保我在编辑 SQL 数据库时没有犯任何错误。我创建了另一个项目并生成了一个身份数据库,我更改了该数据库的连接字符串,但仍然出现相同的错误。

解决方案

当我编辑我的数据库时,我没有注意到在 Identity 2.0.0 中,他们在 AspUserClaims 表中将 User_Id 更改为 UserId。这样做之后,我遇到了同样的错误,但后来我做了 tschmit007 所说的将 ApplicationDbContext 添加到 UserStore 构造函数,现在它可以工作了。

UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

【问题讨论】:

您的ApplicationUser 是否源自IdentityUser?如果不是,它几乎必须。 我猜。它在我的 IdentityUser.cs 中吗?公共类 ApplicationUser : IdentityUser 你用什么代码来实例化你的 UserManager,最重要的是底层的 UserStore? 我已经用声明更新了我的问题 好吧,还有控制器构造函数? 【参考方案1】:

我遇到了同样的问题。我正在使用 EDMX 文件进行数据库优先开发。如果您使用在 :base(“EDMXConnString”) 中添加 EDMX 文件时生成的连接字符串,您很可能会遇到这个问题。

我通过创建一个标准连接字符串来解决此问题,该字符串指向 ASP.NET 标识表所在的数据库。

<add name="MyConnString" connectionString="Data Source=server; Initial Catalog=db_name; User ID=user_id; Password=password; Connect Timeout=60;" providerName="System.Data.SqlClient" />

然后在:base 中使用那个连接字符串,它成功了!

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

    public ApplicationDbContext()
        : base("MyConnString")
    
    

【讨论】:

这也为我解决了这个问题(在数据库优先开发的情况下)。 连接字符串更正有效,但如果有人遇到“无效的列名家乡”错误的问题,只需从 IdentityModels.cs 中删除家乡字段并在 AccountController 中使用,我使用旧脚本在 db 上生成表和我猜他们没有家乡字段,因此我遇到了这个错误...... 2019年已经过去,2020年即将到来。它仍在工作。不错。 工作就像一个魅力,我讨厌不得不创建一个额外的连接字符串。但它仍然有效! @James 这是否意味着我们在 web-config 文件中需要两个连接字符串?一个用于身份模型,一个用于 EDMX 模型?【参考方案2】:

对我来说,它似乎错过了上下文实例:

UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());

应该是

UserManagerFactory = () => new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(new ApplicationDbContext()));

【讨论】:

起初我尝试使用new MyDbContext作为UserStore构造函数的参数,我将尝试使用ApplicationUserDbContext @Marc 对不起,在你的情况下是ApplicationDbContext 支持 'ApplicationDbContext' 上下文的模型自数据库创建以来已更改。我将尝试进行迁移以查看发生了什么变化 迁移代码删除所有内容并从头开始创建我的数据库。正常吗?为什么我有 2 个上下文。 ApplicationDbContext 和我的 myDbContext。我很困惑 我还注意到迁移代码创建了我的 AspNetUser 的旧版本【参考方案3】:

我的问题是我尝试将生成的 ADO.NET 连接字符串用于生成和身份验证上下文 ApplicationDbContext。我通过使用单独的连接字符串进行身份验证来修复它。还要注意提供者 - 对于身份验证上下文,它必须是 System.Data.SqlClient

<add name="DefaultConnection" connectionString="Server=qadb.myserver.com;Database=mydb;User Id=myuser;Password=mypass;" providerName="System.Data.SqlClient" />

【讨论】:

这对我有用 - 它创建了 OWIN 架构和一切 - 谢谢!【参考方案4】:

如果您首先使用代码,请检查您的连接字符串以确保 providerName 为“SqlClient”,如 providerName="System.Data.SqlClient

如果您首先使用数据库,请检查您的连接字符串以确保 providerName 是“EntityClient”,如 providerName="System.Data.EntityClient

【讨论】:

【参考方案5】:

同样的问题,用这段代码解决了:

public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false)

    Database.Connection.ConnectionString = @"data source=...;initial catalog=...;user id=...;password=...;multipleactiveresultsets=True;application name=EntityFramework";

【讨论】:

【参考方案6】:

我也收到此错误消息,但原因和解决方案不同。就我而言,我在 ApplicationUser 类中引入了 Guid 类型的新 Id 属性。完全有效的 C# 语法,但它显然给依赖反射来查找内容的 Identity 或 EntityFramework 核心造成了巨大的混乱。

在我的 ApplicationUser 类中删除新的 Id 属性解决了这个错误。

【讨论】:

【参考方案7】:

我遇到了这个问题,这是一个对象名称冲突。 IdentityConfig.cs 使用的是 ApplicationUser,但它使用的是自动生成的 IdentityModels.ApplicationUser 而不是我自己的上下文的 DataAccess.ApplicationUser。一旦我找到它就很有意义。因此,我从基本 WebAPI 模板中删除了自动生成的 IdentityModels.cs - 无论如何不再使用它 - 然后我将 IdentityConfig.cs 中的 using 语句添加到我自己的 DataAccess 命名空间中,瞧,正确的映射。如果您忘记了为您构建了很多这样的模板,您将遇到问题:

public class ApplicationUserManager : UserManager<ApplicationUser> // the name conflict

    public ApplicationUserManager(IUserStore<ApplicationUser> store)
        : base(store)
    
    

【讨论】:

嗨,经过大量搜索,我发现我有一个类似的问题,您在哪个文件/类中找到了 applicationUser 部分,您删除了哪个【参考方案8】:

我的问题是我创建了一个新的 DbContext,但它不是从 IdentityDbContext 继承的。

一个简单的修复...

public partial class GoldfishDbContext : IdentityDbContext<ApplicationUser>

 ....

【讨论】:

【参考方案9】:

我不确定为什么会发生这种情况,我的解决方案运行良好,在我睡觉前测试了所有内容。 12 小时后,我再次检查并运行,这是完全相同的错误。我在 SO 中尝试了几乎所有的解决方案,但没有一个有效。

我在这里实现了一种数据库方法。然后突然有了这个

默认连接

在我第一次创建解决方案时 Visual Studio 生成的 web.config 上。所以我用它代替了我的 EDMX 文件生成的连接字符串,突然就可以了!

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>

    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    
    

    public static ApplicationDbContext Create()
    
        return new ApplicationDbContext();
       

这是我的有效连接字符串:

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-System.WEB-20180718085411.mdf;Initial Catalog=aspnet-System.WEB-20180718085411;Integrated Security=True" providerName="System.Data.SqlClient" />

最初我使用的是由我的 EDMX 文件生成的,但突然网站无法正常工作,尽管它以前可以工作。我没有更改任何内容,所有代码都在 TFS 中,所以我 100% 确定它可以正常工作,我进行了完全恢复并获得了最新版本:

<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-System.WEB-20180718085411.mdf;Initial Catalog=aspnet-System.WEB-20180718085411;Integrated Security=True" providerName="System.Data.SqlClient" />

【讨论】:

【参考方案10】:

这发生在我身上,因为我试图使用我的依赖注入容器连接 ApplicationUserManager 和其他一些相关的依赖。在某些情况下,容器解析 ApplicationDbContext 在其他情况下,Owin 中的内置注入器会解析它。

确保不会发生这种情况的最简单方法是不要尝试使用您选择的 DI 容器连接任何 Auth 内容,除非您真的知道您在使用 DI 做什么...否则就让 Owin 解决它使用内置注入器。

换句话说,删除以下内容:

 builder.RegisterType<ApplicationUserManager>().InstancePerRequest();

然后让 Owin 以它内置的方式解决它:

 public ApplicationUserManager UserManager
    
        get
        
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        
        private set
        
            _userManager = value;
        
    

【讨论】:

以上是关于实体类型 ApplicationUser 不是当前上下文模型的一部分的主要内容,如果未能解决你的问题,请参考以下文章

ASP.NET MVC 5 - 身份。如何获取当前的ApplicationUser

无法为“ApplicationUser”创建 DbSet,因为此类型未包含在上下文模型中

使用 Clean Architecture 从 ApplicationCore 库中的实体引用 Infrastructure 库中的 ApplicationUser

扩展MVC5和实体框架6 ApplicationUser导致用户注册无效列错误

在 MVC6 中扩展 Identity3

ASP.NET Identity 2.1 和 EF 6 - ApplicationUser 与其他实体的关系