简体   繁体   English

NHibernate派生基类

[英]NHibernate derived base class

we have a NHibernate Entity called Order. 我们有一个称为Order的NHibernate实体。 The Order has a List of Positions and some other stuff. 订单有头寸清单和其他一些东西。

I now implemented a new Entity called OrderMin, which is now a base class of the Order. 我现在实现了一个名为OrderMin的新实体,它现在是Order的基类。 The same I did for the OrderPosition, which has now a base class called OrderPositionMin 我对OrderPosition所做的相同,现在有了一个名为OrderPositionMin的基类

When I try to load an OrderMin-Collection, I get a strange behavior: In the collection, there are now OrderMin and Order objects, this is my Code: 当我尝试加载OrderMin-Collection时,出现一个奇怪的行为:在集合中,现在有OrderMin和Order对象,这是我的代码:

    var mins = Session.QueryOver<OrderMin>()
                    .Where(x => RestrictionExtensions.IsIn(x.Id,
                        list))
                    .List();

When I take a look in the collection, the containing Order objects has now 2 Lists Positions Lists. 当我查看集合时,包含的Order对象现在有2个Lists Positions Lists。 One is from the type OrderPosition and one from OrderPositionMin. 一种来自类型OrderPosition,另一种来自OrderPositionMin。

I tried to use the override keyword in the Order Object, but this is not possible due all Properties must be virtual. 我尝试在Order对象中使用override关键字,但是由于所有属性必须都是虚拟的,因此这是不可能的。

Does anyone have an idea what's going wrong here? 有人知道这里出了什么问题吗?

Thanks in advance, Dennis 预先感谢,丹尼斯

This is a default behavior in NHibernate. 这是NHibernate中的默认行为。 You need to declare a base class from which your Order and OrderMin derive. 您需要声明一个派生Order和OrderMin的基类。

public abstract class OrderBase
{
    // The properties that are needed by all derived types
}

public class OrderMin : OrderBase {}

public class Order : OrderBase {}

After that, your query result should look like you expect it. 之后,您的查询结果应该看起来像您期望的那样。 If you query a base class, NHibernate will materialize all entities of the base class and all entities of the derived types. 如果您查询基类,则NHibernate将实现该基类的所有实体以及派生类型的所有实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM