开发者

NHibernate: Getting "Invalid column name 'ID'." when i use Criteria.List

开发者 https://www.devze.com 2023-01-05 10:36 出处:网络
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)

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开发者_如何学Go I believe).

So why am I getting that error?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消