Featured Article

Mastering Entity Framework Core Migrations for Multiple DbContext Scenarios

Kenneth Jul 13, 2026

When working with multiple databases in a single Entity Framework Core project, you might find yourself needing to manage migrations for each context separately. This can seem daunting at first, but with the right approach, it's quite manageable. Let's dive into how you can effectively handle Entity Framework Core migrations with multiple DbContext.

#dotnet #entityframework #efcore #aspnetcore #backenddevelopment #softwareengineering #webdevelopment #programmingconcepts #techlearning #developercommunity | Sagar Saini
#dotnet #entityframework #efcore #aspnetcore #backenddevelopment #softwareengineering #webdevelopment #programmingconcepts #techlearning #developercommunity | Sagar Saini

Before we start, remember that each DbContext represents a separate database or a certain part of your database. By separating your models into distinct contexts, you gain better control over your database schema and can evolve each part independently. Now, let's explore the intricacies of managing migrations for multiple DbContexts.

Managing Successful SEO Migrations
Managing Successful SEO Migrations

Creating and Managing DbContexts

First, ensure you have created separate DbContext classes for each of your databases or parts of your database. For instance, if you're working with a main database and a separate audit database, you might have 'ApplicationDbContext' and 'AuditDbContext' classes.

a man is pointing to the word data migration on a touch screen with icons and symbols surrounding him
a man is pointing to the word data migration on a touch screen with icons and symbols surrounding him

Each DbContext should have the OnModelCreating method overridden to apply any custom configuration or override behavior.

Seeding DbContexts in Startup.cs

IBM DataStage Migration Paths to Microsoft Fabric, Databricks, Snowflake, Talend & Informatica
IBM DataStage Migration Paths to Microsoft Fabric, Databricks, Snowflake, Talend & Informatica

In your Startup.cs file, ensure you're registering each DbContext separately using AddDbContext<TContext> . This allows the dependency injection container to keep track of each context separately.

```csharp services.AddDbContext(opts => opts.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); services.AddDbContext(opts => opts.UseSqlServer(Configuration.GetConnectionString("AuditConnection"))); ```

Running Migrations

Migrate From Bolt.new To A Custom Codebase: When the Shift Starts Making Sense
Migrate From Bolt.new To A Custom Codebase: When the Shift Starts Making Sense

To run migrations for a specific DbContext, use the dotnet ef command-line tool with the --context parameter. For example, to run migrations for ApplicationDbContext, use:

```bash dotnet ef migrations add InitialCreate --context ApplicationDbContext ```

To apply the migrations, use the following command:

Saeed Esmaeelinejad on LinkedIn: #entityframeworkcore #ef #dotnet #csharp | 30 comments
Saeed Esmaeelinejad on LinkedIn: #entityframeworkcore #ef #dotnet #csharp | 30 comments

```bash dotnet ef database update --context ApplicationDbContext ```

Handling simultaneous updates

Content Migration
Content Migration
Migration and Refugees | Content Map - Lilly Trinh
Migration and Refugees | Content Map - Lilly Trinh
three different types of branches with red and black ink on white paper, each showing the same
three different types of branches with red and black ink on white paper, each showing the same
🔄 Database Migration Software: Best Practices
🔄 Database Migration Software: Best Practices
How to Plan the On-Premise To Cloud Migration Process
How to Plan the On-Premise To Cloud Migration Process
a group of people sitting around a conference table
a group of people sitting around a conference table
the information architecture framework is shown in blue and yellow, as well as several different types of information
the information architecture framework is shown in blue and yellow, as well as several different types of information
Master Data Migration - PiLog Group
Master Data Migration - PiLog Group
eCommerce Replatforming Migration Data: Technical Due Diligence Checklist
eCommerce Replatforming Migration Data: Technical Due Diligence Checklist

When you've evolved both contexts simultaneously, you might face issues due to order-dependent changes. The key here is to order your migrations scripts appropriately.

To ensure migrations scripts are executed in the correct order, use the --ordered flag while adding migrations:

```bash dotnet ef migrations add InitialCreate --context ApplicationDbContext --ordered dotnet ef migrations add InitialCreate --context AuditDbContext --ordered ```

Merging Migrations

Sometimes, you might end up with overlapping changes across migrations scripts for different DbContexts. In such cases, Entity Framework Core provides an option to merge migrations. Use the following command to merge migrations:

```bash dotnet ef migrations merge --context ApplicationDbContext ```

This command combines the changes from multiple migrations into a single script, resolving any overlapping changes.

In managing Entity Framework Core migrations with multiple DbContext, patience and careful planning are key. By following the guidelines above, you'll navigate the process smoothly, ensuring your databases stay up-to-date and your project remains maintainable.

Now that you've insights into handling multiple DbContext migrations, why not explore more advanced topics, such as managing database per environment or employing LiquidBase for automatic schema updates? Keep pushing your Entity Framework Core skills to the next level!