NHibernate动态连接字符串.net核心

时间:2019-05-22 08:48:30

标签: database nhibernate isession

我对在.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();
}

使用动态连接字符串的这种正确方法吗?有效吗?

0 个答案:

没有答案