Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make AddPooledDbContextFactory register DbContext in addition to the and factory #26528

Closed
araxemy opened this issue Nov 3, 2021 · 10 comments · Fixed by #35486
Closed

Make AddPooledDbContextFactory register DbContext in addition to the and factory #26528

araxemy opened this issue Nov 3, 2021 · 10 comments · Fixed by #35486
Assignees
Labels
area-dbcontext closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. type-enhancement
Milestone

Comments

@araxemy
Copy link

araxemy commented Nov 3, 2021

In #25164 it was suggested that EF Core implicitly registers a scoped DbContext as well when AddDbContextFactory is used, which I thought was a great idea, and it was done in RC1 I think.

But why not do the same thing for AddPooledDbContextFactory too?! I'm using AddPooledDbContextFactory in my project and I was surprised to see that the same thing isn't done in that method as well.

@araxemy araxemy changed the title Register a scoped DbContext automatically when using AddPooledDbContextFactory as well Register a scoped DbContext automatically when using AddPooledDbContextFactory as well Nov 3, 2021
@ajcvickers ajcvickers self-assigned this Nov 5, 2021
@ajcvickers ajcvickers added this to the 7.0.0 milestone Nov 5, 2021
@ajcvickers ajcvickers changed the title Register a scoped DbContext automatically when using AddPooledDbContextFactory as well Register a scoped DbContext automatically when using AddPooledDbContextFactory Nov 11, 2021
@msallin
Copy link

msallin commented Dec 28, 2021

Same situation here. We use AddPooledDbContextFactory in all projects but we also use the EF HealthChecks, which expect the DbContext as ctor parameter. Hence, we also have to call AddDbContextPool<T>.

@ajcvickers ajcvickers added propose-punt punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. and removed propose-punt labels Jul 6, 2022
@ajcvickers ajcvickers modified the milestones: 7.0.0, Backlog Jul 7, 2022
@aradalvand
Copy link

aradalvand commented Jul 18, 2022

@ajcvickers Hi there, any reason as to why this was removed from 7.0.0? It seems like it would be relatively straightforward and simple to implement.
Thank you.

@ajcvickers
Copy link
Contributor

@aradalvand We had to punt a number of smaller issues. While each in itself would not take a long time, that time quickly adds up. This one is also only sugar--it's easy to obtain the same functionality by registering trice--which makes it lower priority.

@xamir82
Copy link

xamir82 commented Dec 14, 2022

@ajcvickers Hi, could this please be added to the v8 milestone? It doesn't take too much effort to implement but the API is just incomplete without it. Thanks.

@ErikEJ
Copy link
Contributor

ErikEJ commented Dec 14, 2022

@xamir82 the project accepts pull requests 😉

@ajcvickers
Copy link
Contributor

@xamir82

API is just incomplete without it

Can you explain a bit more what you mean by this? To me, this is a bit of sugar that means people who need this don't have to add an extra line of code to register the service they want.

@ajcvickers ajcvickers removed their assignment Aug 31, 2024
@roji roji changed the title Register a scoped DbContext automatically when using AddPooledDbContextFactory Register both DbContext and factory in DI with a single call Dec 2, 2024
@schmellow
Copy link

schmellow commented Jan 12, 2025

What would be the most correct workaround here?
Would something like that be enough:

builder.Services.AddPooledDbContextFactory<MyContext>( /* full config here */);
builder.Services.AddScoped<MyContext>(p =>
{
    return p.GetRequiredService<IDbContextFactory<MyContext>>().CreateDbContext();
});

(it does not seem wrong to me judging by basic tests, but maybe i'm missing something)
Or do you have to do full AddDbContextPool round, repeating all the configuration?
Or maybe the other way around, as was suggested in here npgsql/efcore.pg#3375 (comment) ?

@roji
Copy link
Member

roji commented Jan 16, 2025

AddDbContextFactory indeed already registers both IDbContextFactory and DbContext in DI (#25164), but AddPooledDbContextFactory indeed does not - I've opened #35484 to track that.

In the meantime, it should be fine to simply also call AddDbContext() after the call - without passing configuration to it (it should pick up the configuration already passed to AddPooledDbContextFactory):

services
    .AddPooledDbContextFactory<BlogContext>(o => o.UseSqlServer(...))
    .AddDbContext<BlogContext>();

@roji roji removed this from the Backlog milestone Jan 16, 2025
@stevendarby
Copy link
Contributor

stevendarby commented Jan 16, 2025

@roji why do you need a new issue? I know the title isn't clear but the first post seems quite clear they are asking for this in AddPooledDbContextFactory, after seeing it in AddDbContextFactory. My only concern is things like up-votes get lost when this happens...

@roji
Copy link
Member

roji commented Jan 16, 2025

You're right, I looked at the title and didn't notice that the original post was specifically talking AddPooledDbContextFactory, and that AddDbContextFactory was working fine. I'll reverse the situation.

@roji roji reopened this Jan 16, 2025
@roji roji marked this as a duplicate of #35484 Jan 16, 2025
@roji roji changed the title Register both DbContext and factory in DI with a single call Make AddPooledDbContextFactory register DbContext in addition to the and factory Jan 16, 2025
@roji roji self-assigned this Jan 16, 2025
@roji roji added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed consider-for-current-release labels Jan 16, 2025
@roji roji added this to the 10.0.0 milestone Jan 16, 2025
roji added a commit to roji/efcore that referenced this issue Jan 16, 2025
@roji roji closed this as completed in 47e5d25 Jan 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-dbcontext closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported punted-for-7.0 Originally planned for the EF Core 7.0 (EF7) release, but moved out due to resource constraints. type-enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants