FluentNHibernate和未识别会话
由于某种原因,当我尝试访问会话时,Nhibernate正在阻塞。它正在引发以下异常: 未配置CurrentSessionContext(将属性设置为当前会话上下文类) 请注意,我没有使用XML来设置配置 我将在测试中打开会话:FluentNHibernate和未识别会话,nhibernate,Nhibernate,由于某种原因,当我尝试访问会话时,Nhibernate正在阻塞。它正在引发以下异常: 未配置CurrentSessionContext(将属性设置为当前会话上下文类) 请注意,我没有使用XML来设置配置 我将在测试中打开会话: [SetUp] public void Initialize() { _session = GetSessionFactory().OpenSession(); _transaction
[SetUp]
public void Initialize()
{
_session = GetSessionFactory().OpenSession();
_transaction = _session.BeginTransaction();
SetupContext();
When();
}
然后我使用存储库访问当前会话。存储库位于不同的dll中
public void Save(Category newCategory)
{
var session = SessionFactory.GetSession();
session.SaveOrUpdate(newCategory);
}
public static ISession GetSession()
{
var session = _sessionFactory.GetCurrentSession();
if (session == null)
return _sessionFactory.OpenSession();
return session;
}
更新:
在我的BaseTest.cs类中,我还有一个拆卸:
[撕裂]
公共空间清理()
{
_session.Dispose();
_transaction.Dispose();
}
在调试期间,似乎正在启动清理并终止_session对象
另一个更新:我在构建配置时添加了以下代码:
public static ISessionFactory CreateSessionFactory()
{
_sessionFactory =
Fluently.Configure().Database(
MsSqlConfiguration.MsSql2000.ConnectionString(
c => c.FromConnectionStringWithKey("ConnectionString")))
.Mappings(m =>
m.FluentMappings.AddFromAssemblyOf<Category>())
**.ExposeConfiguration(x =>
{
x.SetProperty("current_session_context_class",
"thread_static");
})**
.BuildSessionFactory();
return _sessionFactory;
}
公共静态ISessionFactory CreateSessionFactory()
{
_会话工厂=
流利地。配置()数据库(
MsSqlConfiguration.MsSql2000.ConnectionString(
c=>c.FromConnectionString WithKey(“ConnectionString”))
.Mappings(m=>
m、 FluentMappings.AddFromAssemblyOf())
**.ExposeConfiguration(x=>
{
x、 SetProperty(“当前会话上下文类”,
"线程(静态);;
})**
.BuildSessionFactory();
返回工厂;
}
现在我得到以下错误:
没有会话绑定到当前上下文您需要将会话绑定到当前上下文 在
设置中
方法:
var session = SessionFactory.OpenSession();
CurrentSessionContext.Bind(session);
var session = CurrentSessionContext.Unbind(SessionFactory);
session.Dispose();
在拆卸方法中:
var session = SessionFactory.OpenSession();
CurrentSessionContext.Bind(session);
var session = CurrentSessionContext.Unbind(SessionFactory);
session.Dispose();