C
C#4mo ago
Spaxter

✅ Transient DbContext vs DbContextFactory

In EF Core, what is the difference between registering a transient DbContext vs a DbContextFactory for DI? In both cases the DbContext will be created on-demand and disposed when the operation is completed, right? Are there any clear indications on when to use which?
2 Replies
ConfusedPenguin
ConfusedPenguin4mo ago
There isn't a difference as you point out. There are, however, instances where the Factory can provide a service scope for applications where scopes are not possible (Blazor being an example of this). AddDbContext creates the specified DbContext type as a scoped service, where-as the Factory, you can yourself manage the life-time (by determining when to create it).
Spaxter
SpaxterOP4mo ago
Alright, that's the answer I was looking for. Thank you!
Want results from more Discord servers?
Add your server