使用NHibernate映射通用类

时间:2009-11-25 15:47:13

标签: c# .net nhibernate nhibernate-mapping

我正在尝试执行以下操作,但它抱怨“未找到'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继承并全部映射。

1 个答案:

答案 0 :(得分:2)

您无法映射这样的开放泛型类型,即具有未指定类型参数<T>的类型。它只是不起作用。

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