Using Multiple EF Core DbContexts in a Single Application

preview_player
Показать описание

EF Core is the de facto ORM in the .NET community. To work with EF Core, you create a DbContext, representing your database. However, sometimes, you might need multiple EF Core DbContexts in a single application. In this video, I'll show you when you might need this and how to implement it.

Using Multiple EF Core DbContexts In a Single Application:

Join my weekly .NET newsletter:

Read my Blog here:

Chapters
0:00 Adding two EF Core DbContext classes
3:57 Registering the DbContexts with DI
4:52 When do you want to use multiple DbContexts?
6:31 Moving DbContexts into separate schemas and creating migrations
10:10 Fixing the EF migration history table schema
11:38 How to use multiple DbContexts in Minimal APIs
15:35 What you can and CAN'T do with multiple DbContexts
Рекомендации по теме
Комментарии
Автор

I have already used multiple Ef cores, but your lecture is beneficial for me to watch again.

alexmadnix
Автор

Really good one, thanks a lot :) these tips will definitely safe many devs from figuring things out the hard way

rustamhajiyev
Автор

I was coding the base for a modular monolith project, and I saw the notification. Just happiness.

baranacikgoz
Автор

Oh man, excellent! thanks a lot. 🙏🏾
Can't wait for you to do a video on multitenancy.

remyjoseacostamelo
Автор

Greate video! I am junior software dev and this question came to my mind few weeks ago. Cool explenation and clarify example. Just in time to get rid of my concerns :) Thx

dawidbiell
Автор

I have another use-case - create multiple instances of same DbContext (that use different connection strings) to query different shards of your data (meaning each shard is a separate database) in parallel

GlebWritesCode
Автор

I would like to see a deep dive into migrations. Specially changes to a production database and how that would work with changes to the schema of a database.

Thanks for all the Awesome videos!

pcostarg
Автор

My team uses two database contexts. One of the connection strings is configured with the ApplicationIntent=ReadOnly flag. Above these contexts, we have a context manager and it decides which context is to be used depending on the purpose (writing/reading). Both contexts addresses same listener, but connects to different servers: primary (write) and secondary (read).

RobertGaudyn
Автор

Hi Milan. Great videos as always! Thanks for your hardwork and high quality videos. Wondering if you can create a video showing your recommendation to avoid downtimes (or minimize it) when deploying changes in the DB schema with Entity Framework. Thanks in advance.

Автор

When using multiple db contexts I recommend using external tools for database migrations like FluentMigrator or DbUp.
It's really easy to get lost in EF migrations. Before you notice you spend most of your time fighting with your models to generate the right migration's code.

Also it's troublesome to migrate all the db contexts before deploying applications in your release pipeline.

piotrkowalski
Автор

Great tutorial Milan, I love following your videos. I'm curious, in the case of a modular monolith, the creation of an order would occur in the Orders module which would not have access to the ProductsDbContext. In that instance, how would you access the products needed to create an order? Would the Order module query the Products module using a service abstracted using an interface?

disglaceful
Автор

Nice explanation, thanks. I have it set up very similar. Is there any particular way of joining data from two datacontexts (e.g. showing product names in the order)?

borisgomiunik
Автор

🎉 Excellent, I'd like If you can give It a little zoom in on those tiny letters that are hard to read from our side, maybe increasing the environment font size

NagadGames
Автор

Useful content. I nice stuff at the end about drawnbacks and how to use transaction but also you didn't mention that you can use connection from one DbContext in transaction or you can control connection to explicitly through some connection factory.

zokocx
Автор

Hi Milan, Great Video. I have a question. you did not establish navigation prosperity within LineItems and Product Entities. So how to get Order details with the name of the Product in that order?

yasirmehmood
Автор

I have two contexts in the same assembly, and I also have separate Fluent API configurations in different folders. In the OnModelCreating method, I have to manually ignore the entities from the other context. How can I specify configurations based on the folder path and not the assembly? How should this be handled? Thank you!

infotips
Автор

Thanks for sharing the information. One thing, what's your opinion on using DBUp vs EF Core Migration?

microtech
Автор

What if I want to use Separate Db per request. Like getting TenantID from Header with each HTTP Request to generate Connection String and then DbContext ?

MohiyoDeen
Автор

Excellent content! One question that remained and I couldn't find anything specific on the internet, making it necessary to relate tables with different contexts, what would it look like? For example: I created a context to deal with all tables related to user inputs and a context to deal with all system calculation tables, but a relationship is made within the DbContext (in the scenario of a single DbContext) to say that input table A is a foreign key in calculation table B. What would it look like in the case of two DbContext?

liiplima
Автор

I really like the Visual Studio dark theme you are using. Would you share which one it is?

michaelsteffen