【问题标题】:Mapping Generic Classes using NHibernate使用 NHibernate 映射泛型类
【发布时间】:2009-11-26 06:34:53
【问题描述】:

我正在尝试执行以下操作,但它抱怨“未找到 'extends' 引用的类”。我想我需要为每种具体类型的 Component 进行映射,但我不能指定 Attributes.Class 两次..

代码如下:

[NHibernate.Mapping.Attributes.Class(Table = "Components", Abstract = true,
    NameType = typeof (Component<ContentItem>))]
public abstract class Component<T> : IComponent<T> where T : ContentItem
{
    ...
}

[NHibernate.Mapping.Attributes.JoinedSubclass(Table = "ComponentA", ExtendsType = typeof(Component<ItemA>))]
public class ComponentA : Component<ItemA>
{
    ...
}

[NHibernate.Mapping.Attributes.JoinedSubclass(Table = "ComponentB", ExtendsType = typeof(Component<ItemB>))]
public class ComponentB : Component<ItemB>
{
    ...
}

ItemA 和 ItemB 继承自 ContentItem 并且都是映射的。

【问题讨论】:

    标签: c# .net nhibernate nhibernate-mapping


    【解决方案1】:

    您不能像这样映射开放的泛型类型,即具有未指定类型参数 &lt;T&gt; 的类型。它只是不起作用。

    Ayende 在more detail on his blog 中讨论了这个问题。

    【讨论】:

    • 如果我按照那篇文章对 Component`1.hbm.xml 文件中的信息进行硬编码,那么它似乎可以正常工作。我只是不知道如何使用 Mapping.Attributes 注释来做到这一点。烦人的是,T 实际上并没有映射到类中,因为在这个实例中它没有被数据库持久化。
    • 所以听起来 Component 的持久属性实际上并不依赖于 T。如果是这种情况,请将它们分成一个 ComponentBase(已映射),让 Component 派生从 ComponentBase (未映射),然后将您的 Component 类映射为 ComponentBase 的连接子类。
    • 是的,这就是我最终所做的。感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2014-12-09
    • 2011-03-02
    • 2018-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    相关资源
    最近更新 更多