7

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

mejobloggs
  • 7,937
  • 6
  • 32
  • 39

1 Answers1

6

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.

Hamid Mosalla
  • 3,279
  • 2
  • 28
  • 51
  • Are there any disadvantages of running multiple contexts? – mejobloggs Sep 02 '15 at 01:29
  • @mejobloggs Not anything I know about, you have multiple context but same connection string hence same database, on the contrary, I think it's have an advantage of making your entities more manageable. – Hamid Mosalla Sep 02 '15 at 04:54
  • Are you able to provide an example? I can't get my head around having multiple contexts. What happens if I want to query something that uses both contexts? Say... select a sandwhich from mycontext that belongs to a user in the identitycontext? – mejobloggs Sep 02 '15 at 06:28
  • I read a lot of conflicting things about multiple contexts. Some say it's bad practice and causes nightmares, and others say it's normal and good – mejobloggs Sep 02 '15 at 06:52
  • 1
    @mejobloggs I think you have to decide for yourself, I your hypothetical case you store the user Id as a foreign key in sandwich table, and when you want to select for example the current user sandwiches you get the currently logged in user Id through `HttpContext.Current.User.Identity.GetUserId();` and then select your records based on that, you don't need to select anything form identity context directly. – Hamid Mosalla Sep 02 '15 at 07:16
  • Anyone wondering about using two contexts vs one, check here: [How can one put application users in the same context as the rest of the objects?](http://stackoverflow.com/q/19628144/3258851) and here: [ASP.NET Identity DbContext confusion](http://stackoverflow.com/q/19902756/3258851) – Marc.2377 Feb 17 '17 at 04:26