views:

33

answers:

1

Hi all,

I'm using NHibernate with Fluent, and I'm trying to do a GetAll type thing using Critera.List:

    public static List<T> GetAll(int pageIndex, int pageSize)
    {
        using (ISession session = Utils.OpenSession())
        {
            using (ITransaction transaction = session.BeginTransaction())
            {
                ICriteria criteria = session.CreateCriteria(typeof(T));
                criteria.SetFirstResult(pageIndex * pageSize);
                if (pageSize > 0)
                {
                    criteria.SetMaxResults(pageSize);
                }
                return criteria.List<T>() as List<T>;
            }
        }
    }

My map looks like this:

public class GenreMap: ClassMap<Genre>
{
    public GenreMap()
    {
        Table("Genres");
        Id(x => x.ID);
            //.GeneratedBy.Identity();
        Map(x => x.Name, "GenreName")
            .Length(1000);
    }
}

The underlying PK/ID is GenreID (not ID), but I've set the map up correctly (or so I believe).

So why am I getting that error?

+1  A: 

You need to put the column name in for the ID in the map:

Id(x => x.ID, "GenreID"); 

Otherwise NHibernate will think the column name is the same as the property name, which it isn't in this case.

codekaizen
awesome, thanks, that was it. :) for some reason, i thought the Id() method automatically mapped the PK. :)
bryan costanich
@bryan, how could Fluent possibly guess the PK name?
Diego Mijelshon
Cool. Feel free to mark this as the answer!
codekaizen
@Diego - Fluent guesses a lot... I wouldn't be surprised if it just worked, either.
codekaizen
@bryan Fluent does not guess ANYTHING, much less a column name that's different from the property name.
Diego Mijelshon
@Diego - you've never used automapping?
codekaizen
Yes, but you are he's not using automapping there.
Diego Mijelshon
@Diego One might imagine that NHibernate could ask the DB what the primary key column of a given table is. So Fluent would not guess the name, it would ask the DB for that info. But I'm glad that it consistently assumes the DB column name is the same as the property name by default.
apollodude217
@apollo NHibernate does not ask the DB for metadata at any point during SessionFactory construction.
Diego Mijelshon