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

Mysql ASP.NET/Identity错误:实体类型ApplicationUser不是当前上下文模型的一部分,mysql,asp.net,entity-framework,asp.net-identity,Mysql,Asp.net,Entity Framework,Asp.net Identity,在zoidbergi中,他描述了一个类似于我所经历的问题,但没有完全得到回答。我在尝试从内置的.mdf数据库迁移到MySQL时收到上述错误。我使用的是数据库优先的方法,并成功地从底层Mysql数据库创建了实体模型 无论我是否重新创建asp.net identity数据表,只要访问user manager,应用程序就会阻塞: The entity type ApplicationUser is not part of the model for the current context. Desc

在zoidbergi中,他描述了一个类似于我所经历的问题,但没有完全得到回答。我在尝试从内置的.mdf数据库迁移到MySQL时收到上述错误。我使用的是数据库优先的方法,并成功地从底层Mysql数据库创建了实体模型

无论我是否重新创建asp.net identity数据表,只要访问user manager,应用程序就会阻塞:

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
谷歌搜索倾向于找到那些已经放弃并为自己的安全提供商编码的人——如果可能的话,我想使用标准的。莫名其妙

(不雅?)解决方案:

我观看了Alexander Schmidt的这段精彩视频,在33:00时,作者透露连接字符串不应该是EF连接字符串(使用EF提供程序),而应该是专门为安全设置的普通MYSQL/SQLServer连接字符串,即:

<add name="IMSSecurityEntities" connectionString="data source=localhost;database=mydb;Uid=id;Pwd=password;" providerName="mysql.data.mysqlclient"/>

这让我对通过ORM访问安全实体感到紧张,但我想这很可能是出于设计,所以可能不会丢失。

在我的例子中,这是连接字符串的
providerName
属性。我使用了数据库第一次生成的连接字符串的副本,该字符串使用
System.Data.EntityClient
,而常规连接字符串(到SQL Server)使用
System.Data.SqlClient
。所以我改变了这个

<add name="DefaultConnection" connectionString="..." System.Data.EntityClient"/>


奇怪,但却是真的。谢谢这很好-当您尝试迁移到ASP.NET Identity
公共类MySqlDbContext:IdentityDbContext时,IdentityUser也可以使用相同的原则。您将如何在C#中编写此代码?它是否适用于Sql Server?@JustJohn是的,它适用于Sql Server只需在Web.Config中使用System.Data.SqlClient作为providerName
<add name="DefaultConnection" connectionString="..." System.Data.EntityClient"/>
<add name="DefaultConnection" connectionString="..." System.Data.SqlClient"/>