efcore icon indicating copy to clipboard operation
efcore copied to clipboard

AddDbContext: DbContextOptions should have the same ServiceLifetime as TContext

Open jussik opened this issue 10 years ago • 6 comments

Steps to reproduce

Make an ASP.NET Core application with a scoped service that provides a connection string to Entity Framework Core per-request.

services.AddDbContext<MyDbContext>((srv, opts) =>
{
    string cs = srv.GetRequiredService<SomeScopedService>().ConnectionString;
    if (string.IsNullOrEmpty(cs))
        throw new NotSupportedException("No ConnectionString");
    opts.UseSqlServer(cs);
});

The issue

This does not work as expected using the default AddDbContext, it does not return the correct instance of the requested scoped service. If I make my own identical AddDbContext except for registering DbContextOptions and DbContextOptions<TContext> as scoped then it works as expected; the correct instance of the scoped service is accessed and I get my connection string.

Proposal

I propose using the contextLifetime parameter of AddDbContext<TContext> to register DbContextOptions and DbContextOptions<TContext> using the same ServiceLifetime as TContext.

Further technical details

EF Core version: 1.0.1 ASP.NET Core version: 1.0.1 Operating system: Windows 7 Visual Studio version: VS 2015 Update 3

jussik avatar Oct 26 '16 06:10 jussik

@jussik It is intentional that DbContextOptions is registered as a singleton so that the delegate is only executed once and the resultant options are then re-used. While it is not unreasonable to want to instead have the delegate executed every time, we discussed in triage and decided that we're not going to change the behavior since the existing behavior has perf benefits and seems a good default.

There are various things you can do to get the behavior that you want. As you suggested, you can write your own code to add DbContextOptions as scoped--AddDbContext is only sugar for a common pattern and it is always okay to do something custom instead. Alternatively, you could have your DbContext class depend on your scoped service in its constructor and then use OnConfiguring to set the connection string with info from that scoped service.

ajcvickers avatar Oct 31 '16 18:10 ajcvickers

@ajcvickers How would you explain this? I have some DbContext registered. To avoid any scope issue, I tried to use DbContextOptions in a filter, but get an error:

DbContext registration

            services.AddDbContext<TranslationDbContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("TranslateConnection")));
            services.AddTransient<ITranslationDbContext, TranslationDbContext>();
            services.AddSingleton<TranslationService, TranslationService>();

I get this error:

Cannot consume scoped service 'Microsoft.EntityFrameworkCore.DbContextOptions`1[TranslationDbContext]' from singleton 'TranslationService'.

From what you wrote above, it should not be scoped but singleton? The default has changed?

JeanCollas avatar Feb 28 '18 13:02 JeanCollas

@JeanCollas Yes, the default was changed. See PR here: https://github.com/aspnet/EntityFrameworkCore/pull/9009

ajcvickers avatar Feb 28 '18 17:02 ajcvickers

Ok. Changing the default behavior is strange... It means it breaks apps when upgrading the libs. Why not just allow the option to do that without breaking everything?

JeanCollas avatar Feb 28 '18 23:02 JeanCollas

@JeanCollas Because on balance the new default was better, the risk of breaking was quite low, and 2.0 was a new major release that intentionally had many breaking changes over 1.1 to improve architecture and experience in several areas.

ajcvickers avatar Mar 01 '18 00:03 ajcvickers

In my opinion, a good use case for registering a DbContext as a transient dependency is within worker services that are registered as singletons. You can not use scoped dependencies within singleton dependencies. So the only option you have is to register the DbContext as either singleton or transient. Something to bear in mind is that the injected DbContextOptions class lifetime also needs to be updated. You can do both by specifying the service lifetime as follows.

services.AddDbContext<DataContext>(options =>
        {
            options.UseMySQL(configurationRoot.GetConnectionString("DefaultConnection"));
            options.UseLazyLoadingProxies();
        }, ServiceLifetime.Transient, ServiceLifetime.Transient);

The third parameter is for the service lifetime of the DbContextOptions instance.

Hope that helps someone.

caybokotze avatar Jan 30 '22 11:01 caybokotze