Adding Multiple DB Contexts in your DotNet Project

Introduction 

In this article we will discuss adding multiple DB contexts to our DotNet projects, we will use EF core as our ORM to make a "Code First Database" approach. 

In this article example, we will use DotNet 7 connecting to SQL Server through EF Core 7, all of this will be in a web API project to see how to define those DB contexts as services in program.cs file. 
You can download the example source code from here

Why would we need multiple DB Contexts in our Solution? 

There are two situations where you need multiple DB Context in your project or Solution: 
  • We have a single database but need multiple schemas in our database, and each scheme will have a group of entities. 
  • We have Multiple databases and our project needs to connect them to do its operations. 
We will take the first situation as our code example.

Creating Our models

Let's assume we have a simple solution that manages the customer's data, and the employee's data, and for each type of, we need to have two data: hr schema for employee data & crm schema for customer information, first we will create the PersonBase model: 

This model will be a base for the Customer model and Employee model:

We simply inherit the PersonBase properties to both models and add the CustomerNumebr property to the Customer model and the EmployeeNumber property to the Employee model.

Adding DB Contexts

In this stage, we need to create a DB context that defines and maps the models into database entities, for each schema we will create a db context, starting with the CrmDBContext: 

Here we created the db Context, and we also added a DbSet for the Customer model. 

By default, Entity Framework will create the entity (table) for that model under dbo schema, to override this, we will override the OnModelCreating and we will add modelBuilder.HasDefaultSchema("crm"); this will make sure to create all tables for this db context under crm schema. 

The same thing will be done for HrDBContext:

Preparing for Creating Migration files 

To create the migration files, which will be used by Entity Framework to reflect the models into tables in the database, first, we need to make sure that the following packages are added to our project:
  • Microsoft.EntityFrameworkCore.Tools
  • Microsoft.EntityFrameworkCore.SqlServer
Then, define the DB contexts as services on Program.cs file and add a db connection string to it: 

Both context will have the same connection string scinse both contexts will be migrating to the same database. 

Program.cs will read the connection string from appsittings.json file using:
 
in the case of multiple database, we simply use different connection string for each db context. 

 Now, we are ready to create migration files. 

Create Migration files

To create migration file, using package console in Visual Studio, we will run the following command: 

This command will add new migration, first, we will name the migration that we want to create, notice that we added crm_ prefix, this is a best practice to prevent any naming smilirity between the migration classes.

Then, using -project parameter we specified the project where the migration file will be added, here I added it to Infrustructure layer scinse I'm using clean architecture to build my solution. 

Then we will specify for which context we need this migration to generate using -context parameter. 

Finelly, we specify with directory the migration will be added on, by default, EF will add the migration into Migrations folder on the project we specified, but to have a clean manageable migration, we will split the files into different directories based on the contexts using -OutputDir.

Same will be for HR schema db context: 

Conclution

In this Article, we leaned how to add mutlible DB context in DotNet 7, and how we can create multiple schemas using code first approach. 





Comments

Popular posts from this blog

How to Automatically deploy Configuration Files with Azure DevOps CD pipelines

Adapting a New Technology: A General Guide to keep Your Systems up-to-date