I'm using ASP.NET MVC 5 with a Database-First workflow. I've created the Identity tables (AspNetUsers
, AspNetRoles
etc.) in my existing database however I'm having problems getting the register and login functionality to work properly.
This is the IdentityModels.cs
class
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext()
: base("MyConnectionString") // I use "MyConnectionString" instead of "DefaultConnection"
{
}
This is what the EF connection string from the web.config
looks like
<connectionStrings><add name="MyConnectionString" connectionString="metadata=res://*/Entities.MyModel.csdl|res://*/Entities.MyModel.ssdl|res://*/Entities.MyModel.msl;provider=System.Data.SqlClient;provider connection string="data source=My-Pc\SQLEXPRESS;initial catalog=MyExistingDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
And for good measure here's the context class generated from the edmx
public partial class MyConnectionString : DbContext
{
public MyConnectionString()
: base("name=MyConnectionString")
{
}
To me all seems well and it should be able to create users in the database however I'm getting the following error when logging in or trying to register respectively:
For login:
The entity type ApplicationUser is not part of the model for the current context
Line 73: var result = await SignInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
Exception Details: System.InvalidOperationException: The entity type ApplicationUser is not part of the model for the current context.
For register:
The entity type ApplicationUser is not part of the model for the current context
Line 155: var result = await UserManager.CreateAsync(user, model.Password);
Exception Details: System.InvalidOperationException: The entity type ApplicationUser is not part of the model for the current context.
Most articles around seems to be focusing on Code-First and how to go from LocalDb to SQLExpress etc. and how to change the connection string however I haven't been able to solve this problem for a good amount of time.
Edit, solution: As @ChrFin mentioned you could use them both side by side which is what I ended up doing. I simply added a new regular connection string in web.config
and let it point to the existing database. PS. remember that the connection string name cannot be the same as the existing one and must be the same you provide to the ApplicationDbContext
constructor.