Multiple DbContext in a single asp.net mvc applications with EF code first

Is it possible to create multiple DbContext in a single asp.net application with entity framework code first? Yes! Then, How!! This was the questions to me for a couple of weeks. Here is the simple article where you will get all answer. First, I want to say, yes it is possible to create multiple DbContext in a single asp.net application with entity framework code first.

Let’s see how to create multiple DbContext

Tools and Technology used
I used following tools and technology for my project –

  1. Visual Studio 2013
  2. Visual C#
  3. ASP.NET MVC 5
  4. Entity Framework 6

Here, I have an application name PMTool with two DbContext name PMToolContext and SecurityContext.

Step 1: Enable Migration
If you run only Enable-Migrations in package manager console, you will get following error.

PM> Enable-Migrations
More than one context type was found in the assembly 'PMTool'.
To enable migrations for 'PMTool.Repository.SecurityContext', use Enable-Migrations -ContextTypeName PMTool.Repository.SecurityContext.
To enable migrations for 'PMTool.Repository.PMToolContext', use Enable-Migrations -ContextTypeName PMTool.Repository.PMToolContext.

So you have to run enable-migration for each context. You have to specify the directory for each Configuration.cs also. Now run the command in package manager console in the following way.

PM> Enable-Migrations -ContextTypeName PMToolContext -MigrationsDirectory Migrations\PMToolContext
Checking if the context targets an existing database...
Code First Migrations enabled for project PMTool.
PM> Enable-Migrations -ContextTypeName SecurityContext -MigrationsDirectory Migrations\SecurityContext
Checking if the context targets an existing database...
Code First Migrations enabled for project PMTool.

Step 2: Add Migration
Use fully qualified name of the configuration class as follows.

PM> Add-Migration -ConfigurationTypeName PMTool.Migrations.PMToolContext.Configuration FirstMigForPMTool
Scaffolding migration 'FirstMigForPMTool'.

PM> Add-Migration -ConfigurationTypeName PMTool.Migrations.SecurityContext.Configuration FirstMigForSecurity
Scaffolding migration 'FirstMigForSecurity'.

Step 3: Update database

Now update the database. Use fully qualified name of configuration class here also.

PM> Update-Database -ConfigurationTypeName PMTool.Migrations.PMToolContext.Configuration
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201512071619030_FirstMigForPMTool].
Applying explicit migration: 201512071619030_FirstMigForPMTool.
Running Seed method.
PM> Update-Database -ConfigurationTypeName PMTool.Migrations.SecurityContext.Configuration
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201512071627058_FirstMigForSecurity].
Applying explicit migration: 201512071627058_FirstMigForSecurity.
Running Seed method.

If each commands run individually, means you could use multiple DbContext in an asp.net web application with Entity framework. Let’s cheer!!