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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Are there any disadvantages of running multiple contexts?
@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.
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?
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 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.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.