Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET/Identity Error: The entity type ApplicationUser is not part of the model for the current context

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!

like image 770
Rich Freeman Avatar asked Mar 23 '14 15:03

Rich Freeman


1 Answers

(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.

like image 170
Rich Freeman Avatar answered Sep 28 '22 04:09

Rich Freeman