I define two DBContexts in my ef core application, they are in different class,they have the similar constructor, but when i started this project, i got the following exception:
System.AggregateException
HResult=0x80131500
Message=Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: XXX.DefaultDbContext Lifetime: Scoped ImplementationType: XXX.DefaultDbContext': Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[Microsoft.EntityFrameworkCore.DbContext]' while attempting to activate 'XXX.DefaultDbContext'.)
Source=Microsoft.Extensions.DependencyInjection
StackTrace:
at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable`1 serviceDescriptors, ServiceProviderOptions options)
at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(IServiceCollection services, ServiceProviderOptions options)
at Microsoft.Extensions.DependencyInjection.DefaultServiceProviderFactory.CreateServiceProvider(IServiceCollection containerBuilder)
at Microsoft.Extensions.Hosting.Internal.ServiceFactoryAdapter`1.CreateServiceProvider(Object containerBuilder)
at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider()
at Microsoft.Extensions.Hosting.HostBuilder.Build()
at App.Program.Main(String[] args) in
Inner Exception 1:
InvalidOperationException: Error while validating the service descriptor 'ServiceType: XXX.DefaultDbContext Lifetime: Scoped ImplementationType: XXX.DefaultDbContext': Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[Microsoft.EntityFrameworkCore.DbContext]' while attempting to activate 'XXX.DefaultDbContext'.
Inner Exception 2:
InvalidOperationException: Unable to resolve service for type 'Microsoft.EntityFrameworkCore.DbContextOptions`1[Microsoft.EntityFrameworkCore.DbContext]' while attempting to activate 'XXX.DefaultDbContext'.
if i remove the constructor for one of them, or i add an parameterless constructor, then i can start this project, the constructor code looks like below:
public DefaultDbContext()
{
//add this or remove the below constructor can make the application start
}
public DefaultDbContext( DbContextOptions<DefaultDbContext> options) : base(options)
{
}
dose someone knows why?
User contributions licensed under CC BY-SA 3.0