views:

42

answers:

1

I am getting the classic "object reference not set to an instance of a object" error on this line

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

My configure.cs file is as follows

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

in the webconfig i have the connection string

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

With a the Products.mdf in the appcode folder which was build within VS2008

any ideas why i might be getting this issue?

+2  A: 

You could change the method GetSessionFactory() to the following:

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