Change ASP.NET ID to use an existing database

I am using ASP.NET MVC 5 with a Database-First workflow. I have created Identity ( AspNetUsers

, AspNetRoles

etc.) tables in my existing database, however I am having problems getting the register and login functions to work properly.

This is a class IdentityModels.cs

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("MyConnectionString") // I use "MyConnectionString" instead of "DefaultConnection" 
    {
    }

      

This is what the EF connection string from 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 a good mark, here's a context class generated from edmx

public partial class MyConnectionString : DbContext
{
    public MyConnectionString()
        : base("name=MyConnectionString")
    {
    }

      

Everything seems fine to me and it should be able to create users in the database, but I get the following error when registering or trying to register accordingly:

To enter:

The ApplicationUser object type is not part of the model for the current context

Line 73: var result = wait SignInManager.PasswordSignInAsync (model.Email, model.Password, model.RememberMe, shouldLockout: false);

Exception Details: System.InvalidOperationException: The ApplicationUser object type is not part of the model for the current context.

For registration:

The ApplicationUser object type is not part of the model for the current context

Line 155: var result = wait UserManager.CreateAsync (user, model.Password);

Exception Details: System.InvalidOperationException: The ApplicationUser object type is not part of the model for the current context.

Most of the articles around seem to focus on Code-First and how to go from LocalDb to SQLExpress etc. and how to change the connection string, however I have not been able to solve this in a good time.

Edit, solution . As @ChrFin mentioned, you can use them side by side as well as me. I just added a new normal connection string in web.config

and point to the existing database. PS. remember that the name of the connection string cannot be the same as the existing one, and must be the same as for the constructor ApplicationDbContext

.

+3


source to share


2 answers


I THINK this scenario is not supported by ASP.NET Identity as it needs DbContext

one that extends IdentityDbContext<ApplicationUser>

(or similar).



Why are you forcing Identity into your DB-First context?
You can use the Code-First context for Identity along with your other DB-First context without issue ...

+4


source


You can fix this problem by following these steps:



  • 1-create aspnetuser etc. tables in your database (whichever DB you want to use)
  • just connect the application to that database that doesn't use the entity structure, I'm talking just a simple connection.
  • you will find the connection string in your web.config file.

  • put this connection string in clsss identity model

  • now your registration and token methods are executed.
  • now you can use the framewoek entity for the rest of your tables with the first data approach
0


source







All Articles