views:

39

answers:

2

I'm trying to get Fluent and NHibernate configured properly with ASP.NET MVC. As far as I know it's configured properly but when I access the page that uses this setup I am not receiving any data results.

The model I'm using is called Brand and the database table is Brands.

Here is a snippet from my BrandController:

public ActionResult Index()
{
    IRepository<Brand> repo = new BrandRepository();
    var brands = repo.GetAll().ToList<Brand>();

     return View(brands);
}

Here is a snippet from my BrandRepository:

ICollection<Brand> IRepository<Brand>.GetAll()
{
    using (ISession session = NHibernateHelper.OpenSession())
    {
        var brands = session
            .CreateCriteria(typeof(Brand))
            .List<Brand>();

        return brands;
    }
}

Here is my NHibernateHelper:

public class NHibernateHelper
{
    private static ISessionFactory _sessionFactory;

    private static ISessionFactory SessionFactory
    {
        get
        {
            if (_sessionFactory == null)
            {
                _sessionFactory = Fluently.Configure()
                    .Database(
                        MsSqlConfiguration.MsSql2008
                            .ConnectionString(c => c
                                .FromConnectionStringWithKey("ShoesFullAccess")
                            )
                    )
                    .BuildSessionFactory();
            }

            return _sessionFactory;
        }
    }

    public static ISession OpenSession()
    {
        return SessionFactory.OpenSession();
    }
}

Here is my Brand model:

public class Brand
{
    public int Id { get; private set; }
    public virtual string Name { get; set; }
    public virtual IList<Style> Styles { get; private set; }

    public Brand()
    {
        Styles = new List<Style>();
    }

    public virtual void AddStyle(Style style)
    {
        Styles.Add(style);
    }
}

And finally, here is my BrandMap:

public class BrandMap : ClassMap<Brand>
{
    public BrandMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        HasMany(x => x.Styles)
            .Inverse()
            .Cascade.All();
    }
}

If anyone could point me in the right direction for how I can narrow down the problem I would very grateful!

+2  A: 

You are not adding the mappings to the configuration.

Add this before .BuildSessionFactory():

.Mappings(m => m.FluentMappings.AddFromAssemblyOf<BrandMap>())
Diego Mijelshon
You're right. I did have to make this modification! I also had to make a few others.
Joe Philllips
A: 

I needed to make a few modifications to my code. The first and main one was adding the mappings to the configuration as Diego pointed out.

You are not adding the mappings to the configuration.

Add this before .BuildSessionFactory():

.Mappings(m => m.FluentMappings.AddFromAssemblyOf<BrandMap>())

The next thing I had to fix was my Brand model. I needed to make the Id field virtual.

And finally, I needed to change my BrandMap just a little bit -- I had to specify exactly which table it was pointing to by adding this code Table("Brands");

Joe Philllips