首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ASP.NET/Identity错误:实体类型ApplicationUser不是当前上下文模型的一部分

ASP.NET/Identity错误:实体类型ApplicationUser不是当前上下文模型的一部分
EN

Stack Overflow用户
提问于 2014-03-23 23:19:53
回答 2查看 14.8K关注 0票数 15

Move ASP.NET Identity store to EF Sql database中,zoidbergi描述了一个类似于我正在经历的问题,但并没有完全得到回答。当我试图从内置的.mdf数据库迁移到MySQL时,我收到了上面的错误。我使用的是Database-First方法,并且已经成功地从底层Mysql数据库创建了实体模型。

无论我是否重新创建asp.net身份数据表,只要访问用户管理器,应用程序就会阻塞:

代码语言:javascript
复制
The entity type ApplicationUser is not part of the model for the current context.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The entity type ApplicationUser is not part of the model for the current context.

Source Error:  

Line 65:             ' Create a local login before signing in the user
Line 66:             Dim user = New ApplicationUser() With {.UserName = model.UserName}
**Line 67:             Dim result = Await UserManager.CreateAsync(User, model.Password)**
Line 68:             If result.Succeeded Then
Line 69:                 Await SignInAsync(User, isPersistent:=False)

我已经将ApplicationDBContext指向了我的DbContext模型:

代码语言:javascript
复制
Public Class ApplicationDbContext
    Inherits IdentityDbContext(Of ApplicationUser)
    Public Sub New()
        MyBase.New("IMSEntities")
    End Sub
End Class

谷歌倾向于找到那些放弃并编写自己的安全提供商的人-如果可能的话,我想使用标准的安全提供商。莫名其妙!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-03-24 16:20:01

(不优雅?)解决方案:

我观看了Alexander Schmidt的这个不错的视频https://www.youtube.com/watch?v=elfqejow5hM,在33:00,作者透露,连接字符串不应该是EF连接字符串(使用EF提供者),而应该是专门为安全设置的普通MYSQL/SQLServer连接字符串,即:

代码语言:javascript
复制
<add name="IMSSecurityEntities" connectionString="data source=localhost;database=mydb;Uid=id;Pwd=password;" providerName="mysql.data.mysqlclient"/>

同样,身份模型也应该调整为:

代码语言:javascript
复制
Public Class ApplicationDbContext
    Inherits IdentityDbContext(Of ApplicationUser)
    Public Sub New()
        MyBase.New("IMSSecurityEntities")
    End Sub

这让我对通过ORM访问安全实体感到紧张--但我猜很可能是故意的,所以可能没有损失。

票数 30
EN

Stack Overflow用户

发布于 2017-03-22 14:18:16

在我的例子中,它是连接字符串的providerName属性。我使用Database First生成的连接字符串的副本,它使用System.Data.EntityClient,而普通连接字符串(到SQL Server)使用System.Data.SqlClient。所以我改变了这个

代码语言:javascript
复制
<add name="DefaultConnection" connectionString="..." System.Data.EntityClient"/>

到这个

代码语言:javascript
复制
<add name="DefaultConnection" connectionString="..." System.Data.SqlClient"/>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22592770

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档