Fluent NHibernate - 对派生类的查询
假设我有两个类:
public class A
{
public virtual int Id { get; set; }
public virtual Object1 Obj { get; set; }
}
public class B : A
{
public new virtual Object2 Obj { get; set; }
}
我使用 Fluent NHibernate,并且为这两个类创建了两个不同的映射。但是,当我尝试在存储库中查询 A 类时,FNH 会找到 B 类和 A 类,这是有道理的,因为两者都是 A。
示例(此条件将同时查询 A 和 B):
public List<T> GetByName(string name)
{
return Session.CreateCriteriaOf<A>.Add(Restrictions...);
}
在编写 CreateCriteriaOf< 时;A>
,我只想查询 A - 而不是 B。我该如何解决我的问题?
Lets say I have two classes:
public class A
{
public virtual int Id { get; set; }
public virtual Object1 Obj { get; set; }
}
public class B : A
{
public new virtual Object2 Obj { get; set; }
}
I use Fluent NHibernate and I have created two different mappings for the two classes. However, when I try to query class A in my repository, FNH finds both class B and A, which kind of makes sense since both are A.
Example (this criteria will query over both A and B):
public List<T> GetByName(string name)
{
return Session.CreateCriteriaOf<A>.Add(Restrictions...);
}
When writing CreateCriteriaOf<A>
, I only want to query over A - not B. How can I solve my problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你最好创建一个继承树,其中 A 和 B 都派生自公共(抽象)基类型。然后 NHibernate 可以通过鉴别器列进行区分。
当然,您的数据模型应该适应这一点,所以我希望您的模型没有以任何方式规定。
I think you better make an inheritance tree where both A and B derive from a common (abstract) base type. Then NHibernate can make the distinction by a discriminator column.
Of course, your data model should accommodate this, so I hope your model is not prescribed in any way.