开发者

nHibernate 3.0 queries

开发者 https://www.devze.com 2023-01-28 18:37 出处:网络
Working through the summer of nHibernate tutorials have gotten to the section on queries. Seems there have been changes since that series was made. So I went t开发者_运维百科o the online docs for nHB

Working through the summer of nHibernate tutorials have gotten to the section on queries. Seems there have been changes since that series was made. So I went t开发者_运维百科o the online docs for nHB 3.0 but code such as:

IList cats = session.CreateCriteria(typeof(Cat))
            .Add(Expression.Like("Name", "Fritz%"))
            .Add(Expression.Between("Weight", minWeight, maxWeight))
            .List();

Generates the error "The name 'Expression' does not exist in the current context"

Code like:

return session.CreateCriteria(typeof(DataTransfer.Customer))
            .Add(new NHibernate.Criterion.LikeExpression("Firstname", firstname))
            .Add(new NHibernate.Criterion.LikeExpression("Lastname", lastname))
            .List<Customer>();

Works but it seems that it is missing a number of query methods like GtExpression. Are the online docs up to date, and if so, why can't I use Expression... If the online docs aren't up to date then where do I get a description of the Criterion interface? Thanks


You forgot to add using NHibernate.Criterion;.

Anyway, the Expression class is deprecated. Use Restrictions instead.


Weird thing. I still use Expression.* static methods and these are still work. Are you sure you use the latest version of NH3.0? I use Alpha 2 version.

If you need to make it work urgently, let's try the QueryOver<> feature:

return session.QueryOver<DataTransfer.Customer>()
  .WhereRestrictionOn(u => u.Name).IsLike("Fritz%")
  .AndRestrictionOn(u => u.Weight).IsBetween(minWeight).And(maxWeight)
  .List();

It works well for simple queries

0

精彩评论

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

关注公众号