我对在.Net核心应用程序中为动态连接字符串正确配置NHibernate有疑问。
我以Singleton创建了FluentConfiguration:
services.AddSingleton<FluentConfiguration>(factory =>
{
return Fluently.Configure()
.Mappings(x => x.FluentMappings.AddFromAssemblyOf<MappingCandidate>())
.CurrentSessionContext<WebSessionContext>()
.ExposeConfiguration(cfg =>
{
new SchemaExport(cfg)
.Execute(false, false, false);
});
});
ISession范围:
services.AddScoped<ISessionSource, SessionSource>();
和SessionSource如下所示:
public ISession CreateSession(int companyId)
{
return GetSessionFactory(companyId).OpenSession();
}
private Configuration AssembleConfiguration(string connectionString)
{
return _fluentConfiguration
.Database(() =>
{
return FluentNHibernate.Cfg.Db.MsSqlConfiguration
.MsSql2012
.ConnectionString(connectionString);
})
.BuildConfiguration();
}
private ISessionFactory GetSessionFactory(int companyId)
{
var configuration = AssembleConfiguration(__dbCustomProvider.GetConnectionStringForCompany(companyId));
return configuration.BuildSessionFactory();
}
使用动态连接字符串的这种正确方法吗?有效吗?