嗨,我正在爲我的應用程序DI使用Simple Injector。我保留了框架DI的默認DI。用Simple Injector和.Net Core註冊數據庫上下文
但我需要用SimpleInjector註冊DbContext
。當我運行應用程序
container.Verify()
提供了以下錯誤
ActivationException:類型UsersDbContext的構造函數包含參數名爲「選項」和未註冊類型DbContextOptions。請確保DbContextOptions已註冊,或者更改UsersDbContext的構造函數。
我與SimpleInjectore註冊的DbContext在功能SimpleInjectorConfig.InitializeContainer(app, _container)
// DbContext
container.Register<DbContext, UsersDbContext>();
我的啓動是
public void ConfigureServices(IServiceCollection services)
{
var conString = Configuration.GetConnectionString("DefaultConnection");
// Add framework services.
services.AddDbContext<UsersDbContext>(options => options.UseSqlServer(conString));
services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<UsersDbContext>()
.AddDefaultTokenProviders();
IdentityConfigurationService.ConfigureIdentityOptions(services);
services.AddMvc();
// Add application services
services.AddSingleton<IControllerActivator>(new SimpleInjectorControllerActivator(_container));
services.AddSingleton<IViewComponentActivator>(new SimpleInjectorViewComponentActivator(_container));
services.UseSimpleInjectorAspNetRequestScoping(_container);
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
SimpleInjectorConfig.InitializeContainer(app, _container);
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
_container.Verify();
app.UseMvc();
}
我知道問題與選項,但我不知道簡單注射器如何需要註冊到容器的默認連接字符串。這是可以傳遞給簡單注射器的東西嗎?或者我應該使用DbContextFactory
將連接字符串傳遞給UserDbContext
?
這個答案的工作,是我問的答案,但最終我最終交叉佈線DbContext,因爲我已經將它添加到StartUp.cs中的默認DI容器中 –
@JeffFinn你能展示一下你的「交叉佈線」解決方案的片段嗎? – Jeff