Search code examples
c#asp.net.netentity-frameworkasp.net-identity

About DbContext in an ASP.NET Identity environment


Following up this post, I have a question to ask.

If I create the class:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext(): base("DefaultConnection")
    {
        ...
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        ...
    }
}

That means I have to add all my business logic model domain classes into that DbContext or it is possible to add a second one?


Solution

  • It is possible to have another DbContext but the question is whether you want to. See Entity Framework: One Database, Multiple DbContexts. Is this a bad idea?

    So in your case, if the entities in your domain model are separate from the IdentityDbContext then by all means create a new one for those entities. If they rely on anything from IdentityDbContext you will need to use that context for your domain entities. Otherwise you wouldn't be able to query across both.