NHibernate:每个子类映射表和新关键字 [英] NHibernate: Table Per Subclass Mapping and the New Keyword

查看:96
本文介绍了NHibernate:每个子类映射表和新关键字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NHibernate为每个子类继承结构映射一个表,如下所示:

I'm trying to use NHibernate to map a table per subclass inheritance structure that looks something like this:

public class BaseClass
{
    public virtual IColllection<BaseClassCollection> Collection { get; set; }
}

public class ChildClass : BaseClass
{
    public new virtual ICollection<ChildClassCollection> Collection { get; set; }
}

当我尝试从会话中获取ChildClass时,出现以下错误:

I get the following error when I try to get a ChildClass from the session:

无法转换类型为'NHibernate.Collection.Generic.PersistentGenericBag 1[BaseClassCollection]' to type 'System.Collections.Generic.ICollection 1 [ChildClassCollection]'的对象."

"Unable to cast object of type 'NHibernate.Collection.Generic.PersistentGenericBag1[BaseClassCollection]' to type 'System.Collections.Generic.ICollection1[ChildClassCollection]'."

这是我的映射文件:

<class name="BaseClass" table="BaseClassTable">
  <bag name="Collection">
    <key>
      <column name="BaseClassCollectionId" />
    </key>
    <one-to-many class="BaseClassCollection" />
  </bag>
  <joined-subclass name="ChildClass" table="ChildClassTable">
    <key>
      <column name="BaseClassId" />
    </key>
    <bag name="Collection">
      <key>
        <column name="ChildClassCollectionId" />
      </key>
      <one-to-many class="ChildClassCollection" />
  </bag>
</class>

在映射子类时,我需要某种方法来重写基类中Collection属性的类型. NHibernate有可能吗?

I need some way of overriding the type of the Collection property in the base class when I map the child class. Is this possible with NHibernate?

推荐答案

我认为,要使此功能正常运行,唯一的方法就是按照以下方式进行操作:

I think the only way you'll get this to work is if you do something along these lines:

public class BaseClass<TChild> where TChild : BaseClassCollection {
    public virtual ICollection<TChild> Collection { get; set; }
}

public class ChildClass<TChild> : BaseClass<TChild> where TChild : ChildClassCollection {
    public override ICollection<TChild> Collection { get; set; }
}

public class BaseClassCollection { }
public class ChildClassCollection : BaseClassCollection { }

问题在于new关键字破坏了继承,然后您的映射依赖于该继承.您真的应该避免在任何地方使用new,因为它不是一个好主意.将NHibernate排除在外,根据类的投放方式,您将获得不同的行为;将其强制转换为BaseClass,您将获得与将其强制转换为ChildClass完全不同的东西.

The issue is that the new keyword breaks inheritence, and then your mapping is relying on that inheritence. You really should avoid using new anywhere because its not a great idea; take NHibernate out of the mix, and you'll get different behaviors depending on how you cast your class; cast it as BaseClass and youll get something totally different than if you cast it as ChildClass.

如果这不是您要寻找的答案,我表示歉意,但是如果您需要使用new关键字,我认为它不可能正常工作.

If that's not the answer you're looking for I appologize, but I don't think it's possible to get it working if you need to use the new keyword.

这篇关于NHibernate:每个子类映射表和新关键字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆