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

AddDbContext with ServiceLifetime.Scoped should pass scoped IServiceProvider to optionsAction #8797

Closed
prochnowc opened this issue Jun 9, 2017 · 2 comments
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Milestone

Comments

@prochnowc
Copy link

When registering DbContext via AddDbContext(Action<IServiceProvider, DbContextOptionsBuilder>) with ServiceLifetime.Scoped i expect IServiceProvider argument passed to the optionsAction to be IServiceProvider from IServiceScope.

Unfortunately AddDbContext adds DbContextOptions as singleton. Should'nt DbContextOptions be also added scoped when using ServiceLifetime.Scoped ?

In my case i resolve many DbContext's which need to use same DbConnection provided by a scoped IDbConnectionProvider. This enables me to call SaveChanges on different contexts in same transaction.

Steps to reproduce

services.AddScoped<IDbConnectionProvider, DbConnectionProvider>();
services.AddDbContext<DbContextA>(o => (sp, o) => o.UseSqlServer(sp.GetRequiredService<IDbConnectionProvider>().GetConnection());
services.AddDbContext<DbContextB>(o => (sp, o) => o.UseSqlServer(sp.GetRequiredService<IDbConnectionProvider>().GetConnection());

sp argument in AddDbContext should be instance provided by IServiceScope.

Further technical details

EF Core version: 1.1.0
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017

@ajcvickers
Copy link
Contributor

@prochnowc DbContextOptions is intentionally registered as singleton. See #6863 for details.

Note for triage: capturing the scoped service provider is a good idea. I wasn't sure how to do it before, but I think I have an idea now.

@prochnowc
Copy link
Author

Thanks for the reply. I've already worked it around registering DbContextOptions on my own as scoped service.

@ajcvickers ajcvickers reopened this Jun 12, 2017
@ajcvickers ajcvickers added this to the 2.0.0 milestone Jun 12, 2017
@ajcvickers ajcvickers self-assigned this Jun 12, 2017
ajcvickers added a commit that referenced this issue Jun 28, 2017
Issue #8797

After going back and forth on this I settled on:
* Register DbContextOptions as scoped by default
  * This allows scoped services to be used in AddDbContext
  * This allows scoped services to be resolved from context.GetService
  * It will still work fine if scoped services are not needed
* Add another parameter to AddDbContext to allow the options to be registered as Singleton if needed
  * This can be used for apps that are doing some heavy lifting in AddDbContext
ajcvickers added a commit that referenced this issue Jun 29, 2017
Issue #8797

After going back and forth on this I settled on:
* Register DbContextOptions as scoped by default
  * This allows scoped services to be used in AddDbContext
  * This allows scoped services to be resolved from context.GetService
  * It will still work fine if scoped services are not needed
* Add another parameter to AddDbContext to allow the options to be registered as Singleton if needed
  * This can be used for apps that are doing some heavy lifting in AddDbContext
@ajcvickers ajcvickers added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Jun 29, 2017
@ajcvickers ajcvickers removed their assignment Sep 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-enhancement
Projects
None yet
Development

No branches or pull requests

2 participants