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

Ric*_*man 14 mysql asp.net entity-framework asp.net-identity

移动ASP.NET身份存储到EF Sql数据库 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)
Run Code Online (Sandbox Code Playgroud)

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

Public Class ApplicationDbContext
    Inherits IdentityDbContext(Of ApplicationUser)
    Public Sub New()
        MyBase.New("IMSEntities")
    End Sub
End Class
Run Code Online (Sandbox Code Playgroud)

谷歌搜索倾向于找到放弃并编码自己的安全提供者的人 - 如果可能的话,我想使用标准的安全提供者.百思不得其解!

Ric*_*man 28

(INELEGANT?)解决方案:

我观看了Alexander Schmidt的这个优秀视频https://www.youtube.com/watch?v=elfqejow5hM,并在33:00作者发现连接字符串不应该是EF连接字符串(使用EF提供商)但应该是一个专为安全性设置的vanilla MYSQL/SQLServer连接字符串,即:

<add name="IMSSecurityEntities" connectionString="data source=localhost;database=mydb;Uid=id;Pwd=password;" providerName="mysql.data.mysqlclient"/>
Run Code Online (Sandbox Code Playgroud)

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

Public Class ApplicationDbContext
    Inherits IdentityDbContext(Of ApplicationUser)
    Public Sub New()
        MyBase.New("IMSSecurityEntities")
    End Sub
Run Code Online (Sandbox Code Playgroud)

这使我对通过ORM访问安全实体感到紧张 - 但我想可能是设计因此也许没有损失.