C#C
C#β€’15mo agoβ€’
14 replies
Stilau

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 πŸ™‚
Was this page helpful?