Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use the same database for both my custom entities and identity context?

I want to make an MVC website with user logins/authentication etc.

I'm using EF CodeFirst to create my database.

If I create a new MVC project and select Authentication: Individual User Accounts, it will create a new project with an already existing IdentityDbContext etc.

Am I meant to continue using this along with my own DbContext? One context for my projects entities, and the other context for the Identity entities? Does this mean I'll have two separate databases, or can I give them both the same connection string?

I know this may be an open ended question, but are there any good resources/tutorials for AspNet Identity?

So far I've only been finding resources about AspNet Identity itself, and not how to integrate it with the rest of the project/database

like image 687
mejobloggs Avatar asked Sep 01 '15 07:09

mejobloggs


1 Answers

You can specify the same connection string for both of your custom models context and identity context, all you have to do is to change the connection string in the constructor of the ApplicationDbContext class that resides in IdentityModels.cs file like so:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("YouCustomConnectionString", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

and the tables needed for identity will be created in the same database as your other entities, as for resource there is a good set of articles on identity here.

like image 143
Hamid Mosalla Avatar answered Nov 08 '22 03:11

Hamid Mosalla