如何查询特定类型的NHibernate? [英] How to query NHibernate for specific type?

查看:127
本文介绍了如何查询特定类型的NHibernate?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Fluent NHibernate和DiscriminateSubClassesOnColumn()来支持子类化。用于区分子类的列未映射到实体上的实际属性。



如何创建只返回给定类型实体的查询? p>

这里是我的尝试,其中propertyName是我的歧视列的名称,值是类型名称:

  return _db.CreateCriteria< T>()
.Add(Restrictions.Eq(propertyName,value))
.List< T>();

然而,这给了我错误不能解决财产:类型:[我的实体类型] ,这是因为实体本身没有财产。如果我添加属性到我的实体,并映射它,我得到另一个错误:System.IndexOutOfRangeException:这个SqlParameterCollection Count = 7无效的索引7。

解决方案

您将类型传递给通用参数T.例如,如果Cat和Dog扩展抽象类Animal:

  return _db.CreateCriteria< Cat>()
.List< Cat>();

会返回所有的猫

  return _db.CreateCriteria< Animal>()
.List< Animal>();

会返回猫狗。


I'm using Fluent NHibernate with DiscriminateSubClassesOnColumn() to support subclassing. The column used to discriminate between subclasses is not mapped to an actual property on the entity.

How do I create a query which returns only entities of a given type?

Here's my try, where propertyName is the name of my discriminating column and value is the type name:

return _db.CreateCriteria<T>()
            .Add(Restrictions.Eq(propertyName, value))
            .List<T>();

However this gives me the error "could not resolve property: Type of: [my entity type]", which is because the entity itself doesn't have the property. If I add the property to my entity and map it I get another error: "System.IndexOutOfRangeException : Invalid index 7 for this SqlParameterCollection with Count=7."

解决方案

You pass the type to the generic parameter T. For example, if Cat and Dog extend abstract class Animal:

return _db.CreateCriteria<Cat>()
        .List<Cat>();

returns all Cats

    return _db.CreateCriteria<Animal>()
        .List<Animal>();

returns Cats and Dogs.

这篇关于如何查询特定类型的NHibernate?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆