IsessionFactory问题

时间:2010-10-07 19:06:36

标签: asp.net-mvc nhibernate isession

我在这一行上得到了经典的“对象引用未设置为对象的实例”错误

HttpContext.Items["ISession"] = Configure.GetSessionFactory().OpenSession();

我的configure.cs文件如下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;

namespace ForSale.Domain.NHibernate
{
public static class Configure
{
    private static ISessionFactory _sessionFactory;

    public static void Setup()
    {
        _sessionFactory = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008.ConnectionString(cs => cs.FromConnectionStringWithKey("Products")
            ).ShowSql())
            .Mappings(m =>
              m.FluentMappings.AddFromAssemblyOf<Product>().Conventions.AddFromAssemblyOf<Product>())
            .BuildSessionFactory();
    }

    public static ISessionFactory GetSessionFactory()
    {
        return _sessionFactory;
    }
}
}

在webconfig中我有连接字符串

 <connectionStrings>
<add name="Products" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Products.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>

 

使用在VS2008中构建的appcode文件夹中的Products.mdf

为什么我可能会遇到这个问题?

1 个答案:

答案 0 :(得分:2)

您可以将方法GetSessionFactory()更改为以下内容:

public static ISessionFactory GetSessionFactory()
{
    if (_sessionFactory == null)
        Setup();
    return _sessionFactory;
}