currentsessioncontext流利的nhibernate如何做到这一点? [英] currentsessioncontext fluent nhibernate how to do it?

查看:248
本文介绍了currentsessioncontext流利的nhibernate如何做到这一点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用每个请求的会话流利。我正在从nhibernate食谱下面的一个食谱,但它使用nhibernate配置文件。

我不确定什么更好,但现在我坚持流利的配置只是因为我不知道如何设置nhibernate配置文件使用流利的映射和香草nhibernate映射(hbm文件)。

 命名空间Demo.WebUI 
{
public class MvcApplication:NinjectHttpApplication
{
public static ISessionFactory SessionFactory {get;私人设置}
$ b $保护覆盖无效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处理会话上下文。以下可能工作:

pre $ F code $流$ $
$ b $ ExposeConfiguration(cfg = > cfg.SetProperty(
Environment.CurrentSessionContextClass,
web)

另外,与此无关:您正在EndRequest上部署SessionFactory,这是一个错误。


I am trying to use fluent with session per request. I am following a "recipe" from nhibernate cookbook however it uses the nhibernate config file.

I am not sure what is better but right now I am sticking with fluent config just because I would not know how to set the nhibernate config file to use fluent mapping and vanilla nhibernate mapping(hbm files).

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();
        }
    }
}

As you can see in the Begin_Request the books tutorial had

CurrentSessionContext.Bind(session);

However if I use this it throws a error since I don't have the nhibernate config file in use.

So how do I change it to use fluent configuration? Or do I not even need to do this step?(ie is it done internally?)

解决方案

You need to tell NHibernate how to handle the session context. The following might work:

Fluently.Configure()
        ...
        .ExposeConfiguration(cfg => cfg.SetProperty(
                                        Environment.CurrentSessionContextClass,
                                        "web")

Also, unrelated to this: you are disposing the SessionFactory on EndRequest. That is an error.

这篇关于currentsessioncontext流利的nhibernate如何做到这一点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆