开发者

Fluent NHibernate - Query over a derived class

开发者 https://www.devze.com 2023-04-10 20:11 出处:网络
Lets say I have two classes: public class A { public virtual int Id { ge开发者_StackOverflowt; set; }

Lets say I have two classes:

public class A
{
  public virtual int Id { ge开发者_StackOverflowt; 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?


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.

0

精彩评论

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

关注公众号