Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Fluent NHibernate-对派生类的查询_C#_Fluent Nhibernate - Fatal编程技术网

C# Fluent NHibernate-对派生类的查询

C# Fluent NHibernate-对派生类的查询,c#,fluent-nhibernate,C#,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和类

假设我有两门课:

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):

公共列表GetByName(字符串名称)
{
return Session.CreateCriteriaOf.Add(限制…);
}

在编写
CreateCriteriaOf
时,我只想查询A而不是B。如何解决我的问题?

我认为最好创建一个继承树,其中A和B都派生自公共(抽象)基类型。然后NHibernate可以通过一个

当然,您的数据模型应该适应这一点,所以我希望您的模型没有以任何方式规定

public List<T> GetByName(string name)
{
  return Session.CreateCriteriaOf<A>.Add(Restrictions...);
}