Change ASP.NET Identity to use existing database

2019-02-14 02:24发布

问题:

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.

回答1:

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



回答2:

You can resolved this problem by following these steps:

  • 1-create aspnetuser etc tables on your database (whatever DB you want to use)
  • simply connect the application with that database not using entity framework, i'm saying just simple connection.
  • you will find connection string in web.config file.

  • place this connection string into identity model clsss

  • your Register and Token methods now running
  • now you can use entity framewoek for rest of your tables by data first approach