ef核心,两个dbcontext,一个数据库,无法启动项目

时间:2019-12-11 11:41:11

标签: c# .net entity-framework-core

我在ef核心应用程序中定义了两个DBContext,它们在不同的类中,它们具有相似的构造函数,但是当我启动该项目时,出现以下异常:

  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'.

如果我删除其中之一的构造函数,或者添加了无参数构造函数,则可以启动该项目,构造函数代码如下:

    public DefaultDbContext()
    {
       //add this or remove the below constructor can make the application start
    }
    public DefaultDbContext( DbContextOptions<DefaultDbContext> options) : base(options)
    {

    }

有人知道为什么吗?

0 个答案:

没有答案