Bug description
From the documentation EntityFrameworkServiceCollectionExtensions.AddDbContext, the method AddDbContext(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) has the remark paragraph :
This overload has an optionsAction that provides the application's IServiceProvider. This is useful if you want to setup Entity Framework Core to resolve its internal services from the primary application service provider. By default, we recommend using AddDbContext<TContextService,TContextImplementation>(IServiceCollection, Action, ServiceLifetime, ServiceLifetime) which allows Entity Framework to create and maintain its own IServiceProvider for internal Entity Framework services.
The other method AddDbContext(IServiceCollection, Action, ServiceLifetime, ServiceLifetime) set the application service provider because it calls the bellow method, but its haven't the below remark paragraph.
From my tests, it's the case for all methods AddDbContext/AddDbContextFactory.
I didn't test/check for pooled context, but I imagine it's similar.
Your code
- Create a new project with the CLI commands :
dotnet new console
dotnet add package Microsoft.EntityFrameworkCore --version 10.0.10
- Replace Program.cs* by :
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using System.Diagnostics;
internal static class Program
{
static void Main(string[] args)
{
var serviceCollection = new ServiceCollection();
serviceCollection.AddDbContext<MyDbContext>(options => { });
var serviceProvider = serviceCollection.BuildServiceProvider();
var context = serviceProvider.GetRequiredService<MyDbContext>();
}
}
internal sealed class MyDbContext : DbContext
{
public MyDbContext(DbContextOptions options) : base(options)
{
var coreOptionsExtension = options.Extensions.OfType<CoreOptionsExtension>().Single();
Debug.Assert(coreOptionsExtension.ApplicationServiceProvider is null);
}
}
- Run the program
Stack traces
Assertion failed.
coreOptionsExtension.ApplicationServiceProvider is null
at MyDbContext..ctor(DbContextOptions options) in C:\t\ConsoleWithEf\Program.cs:line 31
at System.Reflection.MethodBaseInvoker.InterpretedInvoke_Constructor(Object obj, IntPtr* args)
at System.Reflection.MethodBaseInvoker.InvokeDirectByRefWithFewArgs(Object obj, Span`1 copyOfArgs, BindingFlags invokeAttr)
at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSiteMain(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitRootCache(ServiceCallSite callSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScopeCache(ServiceCallSite callSite, RuntimeResolverContext context)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteVisitor`2.VisitCallSite(ServiceCallSite callSite, TArgument argument)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.Resolve(ServiceCallSite callSite, ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceLookup.DynamicServiceProviderEngine.<>c__DisplayClass2_0.<RealizeService>b__0(ServiceProviderEngineScope scope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(ServiceIdentifier serviceIdentifier, ServiceProviderEngineScope serviceProviderEngineScope)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.GetService(Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType)
at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider)
at Program.Main(String[] args) in C:\t\ConsoleWithEf\Program.cs:line 16
EF Core version
All
Database provider
No response
Target framework
No response
Operating system
No response
IDE
No response
Bug description
From the documentation EntityFrameworkServiceCollectionExtensions.AddDbContext, the method AddDbContext(IServiceCollection, Action<IServiceProvider,DbContextOptionsBuilder>, ServiceLifetime, ServiceLifetime) has the remark paragraph :
The other method AddDbContext(IServiceCollection, Action, ServiceLifetime, ServiceLifetime) set the application service provider because it calls the bellow method, but its haven't the below remark paragraph.
From my tests, it's the case for all methods AddDbContext/AddDbContextFactory.
I didn't test/check for pooled context, but I imagine it's similar.
Your code
Stack traces
EF Core version
All
Database provider
No response
Target framework
No response
Operating system
No response
IDE
No response