我正在尝试使用流畅的会话每个请求.我正在遵循nhibernate cookbook的"配方",但是它使用了nhibernate配置文件.
我不确定什么是更好但是现在我坚持使用流畅的配置只是因为我不知道如何设置nhibernate配置文件以使用流畅的映射和vanilla 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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如您在Begin_Request中可以看到的书籍教程
CurrentSessionContext.Bind(session);
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用它,它会抛出一个错误,因为我没有使用nhibernate配置文件.
那么如何更改它以使用流畅的配置?或者我甚至不需要这样做?(即它是在内部完成的吗?)
我用NHibernate编写了一个C#MVC 3作为ORM,我在大多数页面加载时抛出了一些奇怪的异常.他们似乎主要涉及闭门会议等,我已经检查了大多数常见问题,但没有找到帮助.一些例外包括:
[[NHibernate.Util.ADOExceptionReporter]] : System.InvalidOperationException: There is already an open DataReader associated with this Command which must be closed first.
at System.Data.SqlClient.SqlInternalConnectionTds.ValidateConnectionForExecute(SqlCommand command)
[[NHibernate.Transaction.AdoTransaction]] : Begin transaction failed
System.Data.SqlClient.SqlException (0x80131904): The server failed to resume the transaction. Desc:3b00000006.
[[NHibernate.Transaction.AdoTransaction]] : Commit failed
System.NullReferenceException: Object reference not set to an instance of an object.
[[NHibernate.Transaction.AdoTransaction]] : Commit failed
System.Data.SqlClient.SqlException (0x80131904): The COMMIT TRANSACTION request has no corresponding BEGIN TRANSACTION.
[[NHibernate.Util.ADOExceptionReporter]] : System.InvalidOperationException: The transaction is either not associated with the current …Run Code Online (Sandbox Code Playgroud) c# nhibernate asp.net-mvc session-management session-per-request