Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change ASP.NET Identity to use existing database

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=&quot;data source=My-Pc\SQLEXPRESS;initial catalog=MyExistingDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" 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.

like image 483
brk Avatar asked Sep 16 '14 10:09

brk


1 Answers

I THINK this scenario is not supported by ASP.NET Identity as it needs a DbContext which extends IdentityDbContext<ApplicationUser> (or similar).

Why are you forcing Identity into your DB-First context?
You can use a Code-First context for Identity alongside your other DB-First context without problems...

like image 109
Christoph Fink Avatar answered Sep 30 '22 05:09

Christoph Fink