【发布时间】:2017-08-20 03:49:47
【问题描述】:
我正在将优秀的数据库库 NReco.Data 用于我正在使用 MVC Core 开展的新 MVC 项目。
我正在尝试尽可能多地使用依赖注入。但是,让我困惑的是这个。根据我的 Startup.cs 中的以下代码,我如何实例化另一个指向不同数据库(或连接字符串)的 DbDataAdapter?
public void ConfigureServices(IServiceCollection services)
{
var dbconnectStringAppSettings = Configuration["DBConnectString"];
services.AddDbContext<MisoIRListservDbContext>(options => options.UseSqlServer(dbconnectStringAppSettings));
InjectNRecoDataService(services);
// Repositories
services.AddScoped<ListSettingRepository>();
services.AddScoped<ListCategoryRepository>();
services.AddScoped<MailingListRepository>();
services.AddScoped<LISTSERVRepository>();
// Services
services.AddScoped<ListServEmailMailingListService>();
services.AddScoped<ListCategoryMailingListService>();
services.AddMvc();
}
private void InjectNRecoDataService(IServiceCollection services)
{
services.AddSingleton<IDbFactory, DbFactory>(servicePrv => new DbFactory(SqlClientFactory.Instance)
{
LastInsertIdSelectText = "SELECT @@IDENTITY"
});
services.AddSingleton<IDbCommandBuilder, DbCommandBuilder>(servicePrv =>
{
var dbCmdBuilder = new DbCommandBuilder(servicePrv.GetRequiredService<IDbFactory>());
return dbCmdBuilder;
});
services.AddScoped<IDbConnection>(servicePrv =>
{
var dbCoreContext = servicePrv.GetRequiredService<MisoIRListservDbContext>();
var conn = dbCoreContext.Database.GetDbConnection();
return conn;
});
services.AddScoped<DbDataAdapter>();
}
【问题讨论】:
-
查看多租户数据架构。 benfoster.io/blog/…
标签: c# asp.net-core-mvc .net-core