5

I'm developing web application using ASP.NET Core. When I use project template Web Application with Authentification:

enter image description here

ApplicationDbContext class generated:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser> { public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options) { } } 

I can use it for my modeles, but maybe it's better to create my own DbContext and don't mix my modeles with user's authentification?

2 Answers 2

4

As always, it depends.

Is this a self contained application that your clients are going to deploy on their own servers? If so, then adding the business dtos to the ApplicationDbContext might make sense.

On the other hand, if you're building an enterprisey application, you may want to reuse the authentication across many of your web apps. In that case, you probably would want to separate both the authentication and business models into their own class libraries.

1
  • for now I'll use ApplicationDbContext for everything. I found similar question, and this answer: stackoverflow.com/a/19904081/240564 looks right. Commented Nov 29, 2016 at 15:00
3

I always look at the ApplicationDbContext the same way as the SqlMembershipProvider, I have never added my data access code to it.

ApplicationDbContext is something related to asp.net identity and not to my application or business logic, so I always separate the contexts, have my own user entity and link the other IUser to this entity.

This works fine with me in case if i need to change the whole security framework with something else

3
  • 3
    I would disagree with this, if Microsoft had considered that ApplicationDbContext should only contain models relating to authentication, it would be named something like IdentityDbContext or AuthenticationDbContext. Since it is named **Application**DbContext, I think it is OK to insert other application related models there as long as it makes good design sense. Commented Nov 29, 2016 at 21:40
  • 1
    It is only a default name in a template and should really mean nothing to the design of the application Commented Nov 30, 2016 at 2:18
  • @HaithamShaddad if you look at IdentityDbContext ,you will see it derived from DbContext,also if you have Controller that use DbContext and IdentityDbContext at same time you cannot create instance of both at same time Commented Oct 18, 2018 at 21:09

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.