Using IDbContextFactory and DbContext In The Same

Опубликовано: 23 Февраль 2023
на канале: Codewrinkles
7,490
270

Sometimes we need to work with both the regular DbContext and with IDbContextFactory at the same Asp.Net Core app. Here are two popular use cases:

1️⃣ Blazor Server, when we need to have both components and controllers and Razor Pages in the same project
2️⃣ When running background jobs, like Hangfire.

❗❗❗❗There's however a nasty exception just around the corner. If we try to run the application, we'll get an exception saying that a scoped service can't be consumed in a singleton service.

🤔So, what's wrong? Let me walk you through this.

Join this channel to get source code access and other perks:
   / @codewrinkles  

Also follow me here (especially if you are a self taught developer):
✅My other channel:    / @danpatrascutech  
✅Facebook:   / danpatrascutech  
✅Instagram:   / danpatrascutech  
✅TikTok:   / danpatrascutech  
✅Newsletter: https://www.danpatrascu.tech/

Contents
1. Intro: 00:00
2. Do we even need this? 01:02
3. Throwing Blazor Server in the mix 03:52
4. Annoying exception: 05:13
5. C'mon. it's that simple?! 07:32
6. Conclusion: 09:37

My setup:
Camera - Canon EOS M50 Mark II: https://amzn.to/3SJxS4d
Lav mic - Rode Lavalier GO Professional: https://amzn.to/3mmZS1B
Condenser mic - Shure SM7B: https://amzn.to/3JaqjQN
Audio console - Rodecaster PRO II: https://amzn.to/3KTVMIg
Laptop - Dell Latitude: https://amzn.to/3KV4SEW
Monitors - Benq 27 inch: https://amzn.to/3JbM6aU
Lights - 2x Godox SL-60W: https://amzn.to/3KV3qCj