views:

20

answers:

2

Hi, i have just looked into hooking my application into nhibernate (fluent nhibernate) but i am having a few difficulties...

I have tried to follow what people have done and found this:

public class NHibernateSessionPerRequest : IHttpModule
{
    private static readonly ISessionFactory _sessionFactory;

    static NHibernateSessionPerRequest()
    {
        _sessionFactory = CreateSessionFactory();
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += BeginRequest;
        context.EndRequest += EndRequest;
    }

    public static ISession GetCurrentSession()
    {
        return _sessionFactory.GetCurrentSession();
    }

    public void Dispose() { }

    private static void BeginRequest(object sender, EventArgs e)
    {
        ISession session = _sessionFactory.OpenSession();
        session.BeginTransaction();
        CurrentSessionContext.Bind(session);
    }

    private static void EndRequest(object sender, EventArgs e)
    {
        ISession session = CurrentSessionContext.Unbind(_sessionFactory);

        if (session == null) return;

        try
        {
            session.Transaction.Commit();
        }
        catch (Exception)
        {
            session.Transaction.Rollback();
        }
        finally
        {
            session.Close();
            session.Dispose();
        }
    }

    private static ISessionFactory CreateSessionFactory()
    {
        string connString = "AV8MediaUser";

        FluentConfiguration configuration = Fluently.Configure()
        .Database(MsSqlConfiguration.MsSql2008.ShowSql().ConnectionString(
           x => x.FromConnectionStringWithKey(connString)))
        .ExposeConfiguration(
            c => c.SetProperty("current_session_context_class", "web"))
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Category>());

        return configuration.BuildSessionFactory();
    }
}

But when i run it through it seems to connect to the database correctly but it doesnt run a query:

return Session.CreateCriteria<Category>()
            .List<Category>();

Am i doing something stupidly wrong?

A: 

CreateCriteria expects you to specify a model object name that you'd like to retrieve instances of. You seem to be missing your model object type name. Try something more like this:

    List<YourModelObject> results = session.CreateCriteria<YourModelObject>()
        .List<YourModelObject>();

To see what's actually being sent to the database consider using Ayende's NHProfiler - it will come in handy later when seeing what your more complex criteria queries or HQL queries actually result in...

Tahbaza
I tried that and nothing changed:return Session.CreateCriteria<Category>().List<Category>();Ive also tried NHProfiler... but no session seems to appear when i run my program... which brings me to think the NHibernateSessionPerRequest code i included in the original posting is incorrect somewhere...One thing though, what should be the value of .Mappings(m => m.FluentMappings.AddFromAssemblyOf<THIS?????>()
Gary
A: 

Got it... i didnt realise that my mappings werent included in the project for some unknown reason... but included them again and it is all good!

Weird how it didnt throw an error though.

Gary