如何查询特定类型的NHibernate? [英] How to query NHibernate for specific type?
问题描述
如何创建只返回给定类型实体的查询? 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屋!