0
votes

After, many hours of reading outdated documentation on the Internet for managing session and configuration of NHibernate / Fluent Nhibernate I actually got a configuration that works without using XML, my poco's and map files work in the WebProject and I almost got excited.

However; when I move helper class that returns ISessionFactory to the actual layer it should be in, nothing works, no errors, I get an ISession from the Session Factory just no Entites. Please note I am using the property in this class to get my Factory back, self explanatory I know. Some sort of error would be great to go on here. CODE:

public class NhibernateSessionFactoryHelper
{
    private static ISessionFactory _sessionFactory;

    private static string _connectionString =
        ConfigurationManager.ConnectionStrings["SqlConnectionString"].ToString();
    public static ISessionFactory SessionFactory
    {
        get
        {
            if (_sessionFactory == null)
            {
                _sessionFactory = BuildSessionFactory();
            }
            return _sessionFactory;
        }
    }

    public static ISessionFactory BuildSessionFactory()
    {
        var cfg =
            Fluently.Configure().ProxyFactoryFactory(typeof(ProxyFactoryFactory).AssemblyQualifiedName).Mappings(
                m => m.FluentMappings.AddFromAssemblyOf<Category>()).Database(MsSqlConfiguration.MsSql2008.ConnectionString(_connectionString)).Cache(c => c.UseQueryCache());
        return cfg.BuildSessionFactory();

    }
}

I have ripped out all my Windsor container config to make troubleshooting easier so I have the basic setup as follows.

Web.UI ---> entities (category.cs) ---> mappings (categoryMap.cs) ---> FactoryGoo (NHibernateSessionFactory.cs

CODE For POCO /Entity

public class Category {
    public Category() { }
    public virtual int CategoryID { get; set; }
    public virtual string CategoryName { get; set; }
    public virtual string CategoryDescription { get; set; }
    public virtual System.Nullable<int> ParentCategoryID { get; set; }
    public virtual System.Nullable<int> DisplayOrder { get; set; }
    public virtual int Active { get; set; }
    public virtual string DateCreated { get; set; }
    public virtual string LastUpdated { get; set; }
}

Code for Mapping

public class CategoryMap : ClassMap<Category> {

    public CategoryMap() {
        Table("Categories");
        LazyLoad();
        Id(x => x.CategoryID).GeneratedBy.Identity().Column("CategoryID");
        Map(x => x.CategoryName).Column("CategoryName").Not.Nullable().Length(50);
        Map(x => x.CategoryDescription).Column("CategoryDescription").Not.Nullable();
        Map(x => x.ParentCategoryID).Column("ParentCategoryID");
        Map(x => x.DisplayOrder).Column("DisplayOrder");
        Map(x => x.Active).Column("Active").Not.Nullable();
        Map(x => x.DateCreated).Column("DateCreated").Not.Nullable();
        Map(x => x.LastUpdated).Column("LastUpdated").Not.Nullable();
    }
}

So as I stated earlier, I get my Entity and my data when all of classes live in the same assembly. When I move my SessionFactory to my SessionManagement project or the mapping to the Infrastructure.Data.Mappings project and the entities to the Domain.Entities project nothing works and I gt no error as to why.

Thanks for reading this, I hope I have posted enough for you to get an idea of the setup.

2

2 Answers

0
votes

Make sure the category class referenced in the BuildSessionFactory method is referencing the category class in the expected namespace/project. In other words, there may be multiple category classes in different projects.

0
votes

i'm not sure of course, but I think you meant to link the parent category with your category and that is of the same type? If yes, than use the full blown category object as a property instead of the categoryId, and not use Map() but use fluent References().