NHibernate 和 2 个连接字符串
NHibernate and 2 connectionstrings
目前正在尝试在一个函数中使用 2 个不同的连接字符串。我们有 2 个用户数据库。他们在同一家公司工作,但在 2 座不同的建筑物中。为此,他们建立了 2 个数据库。 A 楼的用户也在 B 楼的用户 table 中,反之亦然。区别在于用户不在他们不工作的建筑物上"active"。
我现在想做的很简单:我检查用户是否存在并在默认数据库中处于活动状态,为结果设置一个变量,然后我做一个简单的 if else 语句并执行 timeregistration procs .
为此我编写了这个函数:
public static RegistrationData LogOnOffCheckBothDBs(int IdType, string userId, string isahUserCode)
{
//To do: Check wether the user is active or not in the default database.
var setting = 0;
RegistrationData Obj = null;
using (ISession session = NHibernateHelper.OpenSession())
{
//userCheckDb == 0 => Database1. als 1 => Database2.
var userCheckDb = session.CreateSQLQuery("exec SIP_KWI_ObsInd_sel :Id, :IdType") //Proc that returns if user is active or not
.SetString("Id", userId)
.SetInt32("IdType", IdType)
.UniqueResult<int>();
setting = userCheckDb;
session.Dispose();
}
if(setting == 1) //If user is not active on default database -> Execute proc in the second connn string (OpenSessionDB2)
{
using (ISession session1 = NHibernateHelper.OpenSessionDb2())
{
var result = session1.CreateSQLQuery("exec SIP_KWI_LogOnOff_prc :Id, :IdType, :IsahUserCode ")
.AddEntity(typeof(RegistrationData))
.SetString("Id", userId)
.SetInt32("IdType", IdType)
.SetString("IsahUserCode", isahUserCode)
.UniqueResult<RegistrationData>();
Obj = result;
}
}
else
{
using (ISession session = NHibernateHelper.OpenSession())
{
var result = session.CreateSQLQuery("exec SIP_KWI_LogOnOff_prc :Id, :IdType, :IsahUserCode ")
.AddEntity(typeof(RegistrationData))
.SetString("Id", userId)
.SetInt32("IdType", IdType)
.SetString("IsahUserCode", isahUserCode)
.UniqueResult<RegistrationData>();
Obj = result;
}
}
return Obj;
}
if 条件都很好,当我放置断点时,一切都应该运行。所以 proc SIP_KWI_ObsInd_sel 工作正常并且 returns 正确的值。
但是,由于某些原因。
if(setting == 1) //If user is not active on default database -> Execute proc in the second connn string (OpenSessionDB2)
{
using (ISession session1 = NHibernateHelper.OpenSessionDb2())
{
var result = session1.CreateSQLQuery("exec SIP_KWI_LogOnOff_prc :Id, :IdType, :IsahUserCode ")
.AddEntity(typeof(RegistrationData))
.SetString("Id", userId)
.SetInt32("IdType", IdType)
.SetString("IsahUserCode", isahUserCode)
.UniqueResult<RegistrationData>();
Obj = result;
}
}
这仍然在错误的数据库(默认数据库)上执行,而不是在我的第二个数据库上执行。
附加信息(Nhibernate 类)
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012
.ConnectionString(c => c.FromConnectionStringWithKey("Isah")).ShowSql()
)
.ExposeConfiguration(
x =>
{
// Increase the timeout for long running queries
x.SetProperty("command_timeout", "180");
// Allows you to have non-virtual and non-public methods in your entities
x.SetProperty("use_proxy_validator", "false");
})
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<Tool>())
.BuildSessionFactory();
}
return _sessionFactory;
}
}
private static ISessionFactory SessionFactoryDB2
{
get
{
if (_sessionFactory == null)
{
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012
.ConnectionString(c => c.FromConnectionStringWithKey("Isah2")).ShowSql()
)
.ExposeConfiguration(
x =>
{
// Increase the timeout for long running queries
x.SetProperty("command_timeout", "180");
// Allows you to have non-virtual and non-public methods in your entities
x.SetProperty("use_proxy_validator", "false");
})
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<Tool>())
.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
public static ISession OpenSessionDb2()
{
return SessionFactoryDB2.OpenSession();
}
您正在为两个会话工厂使用相同的 _sessionFactory
私有字段。为每个创建一个,例如_sessionFactory
和 _sessionFactoryDB2
因此,当您创建 SessionFactoryDB2 时,您对 _sessionFactory == null
的检查将为真,因为它是为 SessionFactory 设置的。您需要 SessionFactoryDB2 的另一个支持字段
private static ISessionFactory SessionFactoryDB2
{
get
{
if (_sessionFactoryDB2 == null)
{
_sessionFactoryDB2 = Fluently.Configure()
目前正在尝试在一个函数中使用 2 个不同的连接字符串。我们有 2 个用户数据库。他们在同一家公司工作,但在 2 座不同的建筑物中。为此,他们建立了 2 个数据库。 A 楼的用户也在 B 楼的用户 table 中,反之亦然。区别在于用户不在他们不工作的建筑物上"active"。
我现在想做的很简单:我检查用户是否存在并在默认数据库中处于活动状态,为结果设置一个变量,然后我做一个简单的 if else 语句并执行 timeregistration procs .
为此我编写了这个函数:
public static RegistrationData LogOnOffCheckBothDBs(int IdType, string userId, string isahUserCode)
{
//To do: Check wether the user is active or not in the default database.
var setting = 0;
RegistrationData Obj = null;
using (ISession session = NHibernateHelper.OpenSession())
{
//userCheckDb == 0 => Database1. als 1 => Database2.
var userCheckDb = session.CreateSQLQuery("exec SIP_KWI_ObsInd_sel :Id, :IdType") //Proc that returns if user is active or not
.SetString("Id", userId)
.SetInt32("IdType", IdType)
.UniqueResult<int>();
setting = userCheckDb;
session.Dispose();
}
if(setting == 1) //If user is not active on default database -> Execute proc in the second connn string (OpenSessionDB2)
{
using (ISession session1 = NHibernateHelper.OpenSessionDb2())
{
var result = session1.CreateSQLQuery("exec SIP_KWI_LogOnOff_prc :Id, :IdType, :IsahUserCode ")
.AddEntity(typeof(RegistrationData))
.SetString("Id", userId)
.SetInt32("IdType", IdType)
.SetString("IsahUserCode", isahUserCode)
.UniqueResult<RegistrationData>();
Obj = result;
}
}
else
{
using (ISession session = NHibernateHelper.OpenSession())
{
var result = session.CreateSQLQuery("exec SIP_KWI_LogOnOff_prc :Id, :IdType, :IsahUserCode ")
.AddEntity(typeof(RegistrationData))
.SetString("Id", userId)
.SetInt32("IdType", IdType)
.SetString("IsahUserCode", isahUserCode)
.UniqueResult<RegistrationData>();
Obj = result;
}
}
return Obj;
}
if 条件都很好,当我放置断点时,一切都应该运行。所以 proc SIP_KWI_ObsInd_sel 工作正常并且 returns 正确的值。
但是,由于某些原因。
if(setting == 1) //If user is not active on default database -> Execute proc in the second connn string (OpenSessionDB2)
{
using (ISession session1 = NHibernateHelper.OpenSessionDb2())
{
var result = session1.CreateSQLQuery("exec SIP_KWI_LogOnOff_prc :Id, :IdType, :IsahUserCode ")
.AddEntity(typeof(RegistrationData))
.SetString("Id", userId)
.SetInt32("IdType", IdType)
.SetString("IsahUserCode", isahUserCode)
.UniqueResult<RegistrationData>();
Obj = result;
}
}
这仍然在错误的数据库(默认数据库)上执行,而不是在我的第二个数据库上执行。
附加信息(Nhibernate 类)
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012
.ConnectionString(c => c.FromConnectionStringWithKey("Isah")).ShowSql()
)
.ExposeConfiguration(
x =>
{
// Increase the timeout for long running queries
x.SetProperty("command_timeout", "180");
// Allows you to have non-virtual and non-public methods in your entities
x.SetProperty("use_proxy_validator", "false");
})
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<Tool>())
.BuildSessionFactory();
}
return _sessionFactory;
}
}
private static ISessionFactory SessionFactoryDB2
{
get
{
if (_sessionFactory == null)
{
_sessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2012
.ConnectionString(c => c.FromConnectionStringWithKey("Isah2")).ShowSql()
)
.ExposeConfiguration(
x =>
{
// Increase the timeout for long running queries
x.SetProperty("command_timeout", "180");
// Allows you to have non-virtual and non-public methods in your entities
x.SetProperty("use_proxy_validator", "false");
})
.Mappings(m =>
m.FluentMappings
.AddFromAssemblyOf<Tool>())
.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
public static ISession OpenSessionDb2()
{
return SessionFactoryDB2.OpenSession();
}
您正在为两个会话工厂使用相同的 _sessionFactory
私有字段。为每个创建一个,例如_sessionFactory
和 _sessionFactoryDB2
因此,当您创建 SessionFactoryDB2 时,您对 _sessionFactory == null
的检查将为真,因为它是为 SessionFactory 设置的。您需要 SessionFactoryDB2 的另一个支持字段
private static ISessionFactory SessionFactoryDB2
{
get
{
if (_sessionFactoryDB2 == null)
{
_sessionFactoryDB2 = Fluently.Configure()