在 Move ASP.NET Identity store to EF Sql database zoidbergi 描述了一个与我遇到的类似的问题,但没有完全回答。我在尝试从内置 .mdf 数据库迁移到 MySQL 时收到上述错误。我正在使用 Database-First 方法,并已成功从底层 Mysql 数据库创建实体模型。
无论我是否重新创建 asp.net 身份数据表,一旦访问用户管理器,应用程序就会阻塞:
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 模型:
Public Class ApplicationDbContext
Inherits IdentityDbContext(Of ApplicationUser)
Public Sub New()
MyBase.New("IMSEntities")
End Sub
End Class
谷歌搜索往往会找到放弃并编写自己的安全提供程序的人 - 如果可能的话,我想使用标准的。莫名其妙!
最佳答案
(不够优雅?)解决方案:
我看了这个优秀的视频 https://www.youtube.com/watch?v=elfqejow5hM Alexander Schmidt 在 33:00 指出连接字符串不应是 EF 连接字符串(使用 EF 提供程序),而应是专门为安全设置的 vanilla MYSQL/SQLServer 连接字符串,即:
<add name="IMSSecurityEntities" connectionString="data source=localhost;database=mydb;Uid=id;Pwd=password;" providerName="mysql.data.mysqlclient"/>
同样,身份模型应调整为:
Public Class ApplicationDbContext
Inherits IdentityDbContext(Of ApplicationUser)
Public Sub New()
MyBase.New("IMSSecurityEntities")
End Sub
这让我对通过 ORM 访问安全实体感到紧张 - 但我想这很可能是设计使然,所以可能没有损失。
关于mysql - ASP.NET/身份错误 : The entity type ApplicationUser is not part of the model for the current context,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22592770/