我正在尝试根据请求使用流利的会话。我正在遵循 nhibernate 食谱中的“食谱”,但它使用 nhibernate 配置文件。
我不确定什么更好,但现在我坚持使用流畅的配置,只是因为我不知道如何设置 nhibernate 配置文件以使用流畅的映射和普通 nhibernate 映射(hbm 文件)。
namespace Demo.WebUI
{
public class MvcApplication : NinjectHttpApplication
{
public static ISessionFactory SessionFactory { get; private set; }
protected override void OnApplicationStarted()
{
SessionFactory = Fluently.Configure()
.Database(MsSqlConfiguration.MsSql2008.ConnectionString(
c => c.FromConnectionStringWithKey("test")))
.Mappings(m => m.FluentMappings
.AddFromAssemblyOf
<Demo.Framework.Data.NhibernateMapping.UserMap>())
.ExposeConfiguration(BuidSchema)
.BuildSessionFactory();
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
var session = SessionFactory.OpenSession();
//CurrentSessionContext.Bind(session);
}
protected void Application_EndRequest(object sender, EventArgs e)
{
//var session = CurrentSessionContext.Unbind(SessionFactory);
SessionFactory.Dispose();
}
}
}
正如您在 Begin_Request 中看到的,书籍教程有
CurrentSessionContext.Bind(session);
但是,如果我使用它,它会抛出一个错误,因为我没有使用 nhibernate 配置文件。
那么如何更改它以使用流畅的配置呢?或者我什至不需要执行此步骤?(即它是在内部完成的吗?)
您需要告诉 NHibernate 如何处理会话上下文。以下方法可能有效:
Fluently.Configure()
...
.ExposeConfiguration(cfg => cfg.SetProperty(
Environment.CurrentSessionContextClass,
"web")
另外,与此无关:您正在 EndRequest 上处置 SessionFactory。这是一个错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)