Multiple Provider in EntityFramework Core and Migrations

Hi, We will managing how to add multiple provider in ef core, like postgresql, sql server and sqllite

Starting Project

I'm creating as .net 7.0 web api in vs 2022

I'm creating folder called Entites to Domain and add two entity: User and Book

User and Book entities:

We are adding UserContext and BookContext in Data structure

UserContext runs on SqlServer BookContext runs on Postgresql

Optional Configuration for User Entity

Setting Multiple Provider

We will using Npgsql provider for postgresql. We will register Contexts in Api structure. Before fill connection string to appSettings.json

Program.cs:

Create Migration

We will create migrations with Powershell in Project Root Path. Done Before We look tags.

–project: Storage Migrations Project
–output-dir: Migrations Folder Path
–context : The Context
–startup-project: Project in where Context is registered

UserContext migration:

BookContext migration

Created Migrations:

Apply Migrations:

There is two method. The first is update-database, Other is in runtime.

1.Update-Database

2.Runtime

Example Insert:

Project's Github Url okankrdg/EfCoreMultipleProvider (github.com)

See you in the next article, with love

You may also like...

Leave a Reply

Your email address will not be published.

English