3

I have a small POC application that I am working with, using nHibernate. This is my first time setting nHibernate up on my own, but I have worked with it before. For some reason, my queries are not returning any data. I can confirm I have one Product in my database.

public class NHibernateHelper
{
    private static String _connectionString =
        @"Server=localhost\SQLEXPRESS;Database=TestProject;User ID=TestProjectMvc;Password=Pa$$word";

    private static ISessionFactory _sessionFactory;

    private static ISessionFactory SessionFactory
    {
        get
        {
            if (_sessionFactory == null)
                InitializeSessionFactory();

            return _sessionFactory;
        }
    }

    private static void InitializeSessionFactory()
    {
        _sessionFactory = Fluently.Configure()
            .Database(MsSqlConfiguration.MsSql2008.ConnectionString(_connectionString).ShowSql()
            ).Mappings(m => m.FluentMappings.AddFromAssemblyOf<Product>())
            //.ExposeConfiguration(cfg => new SchemaExport(cfg).Create(true, true))
            .BuildSessionFactory();
    }

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

My mapping class:

 public class ProductMap : ClassMap<Product>
 {
    public ProductMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        References(x => x.Category).Column("CategoryId");
    }
 }

And the method I am using to retrieve the data:

    public IEnumerable<Product> GetAllProducts()
    {
        using (var session = NHibernateHelper.OpenSession())
        {
            var list = session.QueryOver<Product>().List();
            return list;
        }
    }
2
  • 1
    Are your mappings and domain objects in the same assembly? Commented Oct 24, 2013 at 22:08
  • @SimonWhitehead Ahh that was it... I changed my Mappings to load from ProductMap assembly and I am seeing the data now. Thanks! If you want, post your answer and i'll mark it as such. Commented Oct 24, 2013 at 22:12

2 Answers 2

5

The incredibly helpful method for adding mappings from an assembly of another type always trips others up.

For anyone else wondering.. the issue is this:

.AddFromAssemblyOf<Product>()

Product and ProductMap were in different assemblies. So the mappings cannot be found in the assembly that Product resides in.

I have seen this trip up many people starting out!

Sign up to request clarification or add additional context in comments.

1 Comment

Nice explanation! Haha most people wouldn't put this much effort into writing an answer they already know will get marked as THE answer. Kudos!
0

Also - if you use a SchemaExport to generate the database tables for you - /do/ remember to turn that off the 2nd time you run the application. Otherwise it's blank tables all over again for you...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.