C# Fluent NHibernate,加入子类映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/318087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fluent NHibernate, joined-subclass mapping
提问by Martin Faartoft
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
我正在尝试使用 Fluent NHibernate 映射一个加入的子类场景。我在命名空间 Core 中定义了一个类 Entity,在命名空间 SomeModule 中有一个类 SubClass : Entity
Now I obviously don't want class Entity to know about its derived types, the SomeModules namespace references Core - not the other way around.
现在我显然不希望类 Entity 知道它的派生类型, SomeModules 命名空间引用 Core - 而不是相反。
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:
这在我的情况下根本不起作用 - 我需要类似于 NHibernate xml 映射的东西:
<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?
有没有人用 Fluent NHibernate 实现过这个?
回答by Magnus Bertilsson
Hello did some thing like it a few days ago.
你好几天前做了一些类似的事情。
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);
});
}
}
Hope it helps
希望能帮助到你
回答by chris raethke
Magnus, I was having the exact same type of issue, and your suggestion sorted it.
马格努斯,我遇到了完全相同类型的问题,你的建议解决了它。
The second parameter to JoinedSubClass takes a SubClassPart Action against your object of type SubT. Hence you only need to then map the additional fields on your subclassed object.
JoinedSubClass 的第二个参数对您的 SubT 类型的对象执行 SubClassPart Action。因此,您只需要映射子类对象上的附加字段。
That previous example is mapping an Id, so i'm guessing thats a different id to the value the base and subclass join on, otherwise you'd start to see SqlParameterCollection errors being through.
前面的例子是映射一个 Id,所以我猜这是一个与基类和子类连接的值不同的 id,否则你会开始看到 SqlParameterCollection 错误正在通过。
回答by Magnus Bertilsson
Sorry missed your comment, found this
抱歉错过了你的评论,找到了这个
public class SubClassMap : JoinedSubClassPart< SubClass >
{
public SubClassMap()
: base("SubClassId")
{
Map(x => x.Name);
Map(x => x.SomeProperty);
}
}
Hope it helps if you haven't solved it already.
如果你还没有解决它,希望它会有所帮助。
回答by Nagyman
Magnus (or Prise),
马格努斯(或普瑞斯),
Did you figure out how to use that last example in the parent mapping class? This worked for me but I don't like instantiating the SubClassMap myself:
您是否弄清楚如何在父映射类中使用最后一个示例?这对我有用,但我不喜欢自己实例化 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);
}
}
Which produced a query similar to:
这产生了类似于以下内容的查询:
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
回答by Sean Lynch
I think the API has changed since this question was asked, but this works for me:
我认为自从提出这个问题以来 API 已经发生了变化,但这对我有用:
public class SomeSubclassMap : SubclassMap<SomeSubclass> {
public SomeSubclassMap()
{
KeyColumn("SomeKeyColumnID");
Map(x => x.SomeSubClassProperty);
...
}
}
I believe KeyColumn is only required if it's different than 'Baseclassname_id'
我相信 KeyColumn 仅在与“Baseclassname_id”不同时才需要
Note: There should also be a ClassMap<SomeBaseClass>
for the base class that SomeSubClass extends.
注意:ClassMap<SomeBaseClass>
SomeSubClass 扩展的基类也应该有一个。