System.ArgumentException HResult=0x80070057 Message=AddDbContext was called with configuration, but the context type 'MyDBContext' only declares a parameterless constructor.
This means that the configuration passed to AddDbContext will never be used.
If configuration is passed to AddDbContext, then 'MyDBContext' should declare a constructor that accepts a DbContextOptions<MyDBContext> and must pass it to the base constructor for DbContext. Source=Microsoft.EntityFrameworkCore StackTrace: at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.CheckContextConstructors[TContext]() at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext[TContextService,TContextImplementation](IServiceCollection serviceCollection, Action`2 optionsAction, ServiceLifetime contextLifetime, ServiceLifetime optionsLifetime) at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext[TContextService,TContextImplementation](IServiceCollection serviceCollection, Action`1 optionsAction, ServiceLifetime contextLifetime, ServiceLifetime optionsLifetime) at Microsoft.Extensions.DependencyInjection.EntityFrameworkServiceCollectionExtensions.AddDbContext[TContext](IServiceCollection serviceCollection, Action`1 optionsAction, ServiceLifetime contextLifetime, ServiceLifetime optionsLifetime) at WebApi1.Startup.ConfigureServices(IServiceCollection services) in C:Userspxmsource eposWebApi1WebApi1Startup.cs:line 31 at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.InvokeCore(Object instance, IServiceCollection services) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass9_0.<Invoke>g__Startup|0(IServiceCollection serviceCollection) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.Invoke(Object instance, IServiceCollection services) at Microsoft.AspNetCore.Hosting.ConfigureServicesBuilder.<>c__DisplayClass8_0.<Build>b__0(IServiceCollection services) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.UseStartup(Type startupType, HostBuilderContext context, IServiceCollection services) at Microsoft.AspNetCore.Hosting.GenericWebHostBuilder.<>c__DisplayClass12_0.<UseStartup>b__0(HostBuilderContext context, IServiceCollection services) at Microsoft.Extensions.Hosting.HostBuilder.CreateServiceProvider() at Microsoft.Extensions.Hosting.HostBuilder.Build() at WebApi1.Program.Main(String[] args) in C:Userspxmsource eposWebApi1WebApi1Program.cs:line 16
翻译:
已使用配置调用AddDbContext,但上下文类型“MyDBContext”仅声明一个无参数构造函数。这意味着传递给AddDbContext的配置将永远不会被使用。
如果将配置传递给AddDbContext,那么“MyDBContext”应该声明一个接受DbContextOptions<MyDBContext>的构造函数,并且必须将其传递给DbContext的基本构造函数。
解决方法:
MyDBContext添加一个DbContextOptions<MyDBContext>类型参数的构造函数