C
C#β€’2mo ago
StilauGamer

Inject IDbContextFactory or DbContext directly?

Hey, I was working with a project earlier that I've made and I saw I was injecting the db context with IDbContextFactory<ApplicationDbContext> and then manually creating a db context every time I was going to use it. I was doing this: await using var dbContext = await _dbContextFactory.CreateDbContextAsync(); I read somewhere that you should just inject the DB Context directly, and let the DI handle the lifetime of it, but I was unsure if this was bs or not? Does anyone have any tips surrounding this? Thanks πŸ™‚
8 Replies
Salman
Salmanβ€’2mo ago
yes register it in the DI container and then inject it and use it wherever you want. I've never seen that factory stuff seems like some legacy for example in Program.cs before building the app you register it like this:
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
builder.Services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(connectionString));
And then inject it using constructors wherever you want
StilauGamer
StilauGamerOPβ€’2mo ago
So I should just inject the DB Context directly, and not use the IDbContextFactory part πŸ€“ I was confused myself, but I found it somewhere and it told me that I should use that for pooling, etc I was using this implementation: builder.Services.AddPooledDbContextFactory<ApplicationDbContext>(optionsBuilder => { }); I don't remember exactly where, but I read that you should use the factory part of EF, or smth
Salman
Salmanβ€’2mo ago
I've never seen it, normally you register it like I showed you above
Salman
Salmanβ€’2mo ago
DbContext Lifetime, Configuration, and Initialization - EF Core
Patterns for creating and managing DbContext instances with or without dependency injection
StilauGamer
StilauGamerOPβ€’2mo ago
From my understanding, the DB Context Factory is more widely used within Blazor applications where the scope is more wide... The scope is per user request and not just a HTTP request like in a Web API.. So its more recommended to manually create the db context, etc in Blazor. But in a Web API, you are good enough with just injecting the DB Context directly as its scoped to that request only.. Thats from what I understood. Reference to the article
Stack Overflow
Difference between AddDbContext and AddDbContextFactory
Actually I started with Blazor and EF Core. At registering the DbContext i get stucked. The DbContext can be registerd with AddDbContext or with AddDbContextFactory. But whatΒ΄s the difference? buil...
Salman
Salmanβ€’2mo ago
right check this part in the docs: https://learn.microsoft.com/en-us/ef/core/dbcontext-configuration/#use-a-dbcontext-factory Although I never had any problem with Blazor and I use DBContext without factory
StilauGamer
StilauGamerOPβ€’2mo ago
Yeah, its just that you use the same db context over the whole user request.. In Blazor, 1 scoped service last from when he starts viewing till he closes / refreshes, etc.. So in theory, when using a db context, you have it alive for the whole time, if its not disposed at some point tho ofc.. But then I don't need to do it in my web apis :sunglas: Another mystery solved for today! 🫑
sibber
sibberβ€’2mo ago
the factory is for when you need multiple units of work or when the lifetime of the context doesnt fit e.g. if youre writing a factory for something that needs DbContexts otherwise just inject DbContext directly
Want results from more Discord servers?
Add your server