8

I'm trying to map a joined-subclass scenario using Fluent NHibernate. I have a class Entity defined in the namespace Core, and a class SubClass : Entity in the namespace SomeModule

Now I obviously don't want class Entity to know about its derived types, the SomeModules namespace references Core - not the other way around.

All the examples I've been able to find use something like:

public class EntityMap : ClassMap<Entity> {
    public EntityMap() {
        Id(x => x.Id)

        var subClassMap = JoinedSubClass<SubClass>("SubClassId", sub => sub.Map(x => x.Id));

        subClassMap.Map(x => x.SomeProperty)
        ...
    }
}

This simply won't work in my situation - I need something akin to the NHibernate xml mapping:

<joined-subclass name="SubClass" extends="Entity, Core" >
<key column="SubClassId" foreign-key="FK_KollegiumEntity"/>
<property name="Name" length="255" not-null="true" />
...
</joined-subclass>

Has anyone achieved this with Fluent NHibernate?

4

5 回答 5

10

我认为自从提出这个问题以来API已经改变,但这对我有用:

public class SomeSubclassMap : SubclassMap<SomeSubclass> {
    public SomeSubclassMap()
    {
        KeyColumn("SomeKeyColumnID");
        Map(x => x.SomeSubClassProperty);
        ...
    }
}

我相信只有 KeyColumn 与“Baseclassname_id”不同时才需要它

ClassMap<SomeBaseClass>注意: SomeSubClass 扩展的基类也应该有一个。

于 2010-05-06T15:52:45.117 回答
1

抱歉错过了你的评论,找到了这个

public class SubClassMap : JoinedSubClassPart< SubClass >
{
    public SubClassMap()
        : base("SubClassId")
    {
         Map(x => x.Name); 
         Map(x => x.SomeProperty); 
    }
}

如果您还没有解决它,希望它会有所帮助。

于 2008-12-17T09:18:55.223 回答
1

马格努斯(或普瑞斯),

您是否知道如何在父映射类中使用最后一个示例?这对我有用,但我不喜欢自己实例化 SubClassMap:

public class EntityMap : ClassMap<Entity> {
 public EntityMap() {
    Id(x => x.Id)
    AddPart(new SubClassMap()); // Adds the subclass mapping!
 }
}

public class SubClassMap : JoinedSubClassPart<SubClass>
{
    public SubClassMap()
        : base("SubClassId")
    {
         Map(x => x.Name); 
         Map(x => x.SomeProperty); 
    }
}

这产生了一个类似于以下内容的查询:

SELECT
  this_.Id as Id2_2
  this_.Name as Name3_2
  this_.SomeProperty as SomeProperty3_2
FROM
  SubClass this_ inner join
    Entity this_1 on this_.Id=this_1.Id
于 2009-01-06T18:19:27.570 回答
0

Magnus,我遇到了完全相同类型的问题,您的建议对其进行了排序。

JoinedSubClass 的第二个参数对您的 SubT 类型的对象执行 SubClassPart 操作。因此,您只需要在子类对象上映射其他字段。

前面的例子是映射一个Id,所以我猜这是一个与基类和子类连接的值不同的id,否则你会开始看到SqlParameterCollection 错误正在发生。

于 2008-12-05T15:12:52.517 回答
0

你好几天前做了类似的事情。

public class EntityMap : ClassMap<Entity> {
 public EntityMap() {
    Id(x => x.Id)

    JoinedSubClass<SubClass>("SubClassId", sub => { 
          sub.Map(x => x.Name); 
          sub.Map(x => x.SomeProperty); 
    });
 }
}

希望能帮助到你

于 2008-11-27T15:29:56.837 回答