ASP.NET/Identity Error: The entity type Applicatio

2019-02-08 15:23发布

In Move ASP.NET Identity store to EF Sql database zoidbergi described an issue similar to that which I'm experiencing, but which was not completely answered. I am receiving the error above when attempting to migrate from the inbuilt .mdf database to MySQL. I am using a Database-First methodology and have successfully created the Entity Model from the underlying Mysql database.

No matter whether I recreate the asp.net identity datatables the application chokes as soon as user manager is accessed:

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)

I have pointed the ApplicationDBContext at my DbContext model:

Public Class ApplicationDbContext
    Inherits IdentityDbContext(Of ApplicationUser)
    Public Sub New()
        MyBase.New("IMSEntities")
    End Sub
End Class

Googling tends to find people who have given up and coded up their own security providers - I'd like to use the standard one if possible. Baffled!

2条回答
欢心
2楼-- · 2019-02-08 16:01

In my case it was the providerName property of the connection string. I used a copy of Database First generated connection string which uses System.Data.EntityClient while normal connection strings (to SQL Server) use System.Data.SqlClient. So I changed this

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

to this

<add name="DefaultConnection" connectionString="..." System.Data.SqlClient"/>
查看更多
该账号已被封号
3楼-- · 2019-02-08 16:09

(INELEGANT?) SOLUTION:

I watched this excellent video https://www.youtube.com/watch?v=elfqejow5hM by Alexander Schmidt and at 33:00 the author reveals that the connection string should not be an EF connection string (using the EF provider) but should be a vanilla MYSQL/SQLServer connection string specifically set up for security, ie:

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

and similarly the identity model should be adjusted to:

Public Class ApplicationDbContext
    Inherits IdentityDbContext(Of ApplicationUser)
    Public Sub New()
        MyBase.New("IMSSecurityEntities")
    End Sub

This makes me nervous about accessing the security entities through the ORM - but I guess may well be by design so maybe no loss.

查看更多
登录 后发表回答