Fluent NHibernate 继承问题
我正在使用 Nhibernate 和 Fluent,但遇到了继承问题。
这里是我的数据库模式
TABLE Base
- IDBASE (PK)
- Field1
- TYPE
TABLE T1
- IDBASE (PK and FK)
- Field2
- Field3 ...
我的映射文件是:
public class BaseMap: ClassMap<BASE>
{
public BaseMap()
{
Id(x => x.Id, "IDBASE").GeneratedBy.Identity();
Map(x => x.Field1);
DiscriminateSubClassesOnColumn("TYPE");
}
}
public class T1Map: SubclassMap<T1>
{
public T1Map()
{
Table("T1");
KeyColumn("IDBASE");
DiscriminatorValue("T1");
Map(x => x.Field2).Not.Nullable();
Map(x => x.Field3).Not.Nullable();
}
}
我使用 FluentMappings 而不是 AutoMapping。
这里我的实体:
public abstract class BASE
{
public virtual long IdBase{ get; set; }
public virtual string Field1 { get; set; }
}
public class T1: BASE
{
public virtual string Field2 { get; set; }
public virtual string Field3 { get; set; }
}
T1 实体继承自 BASE 实体,问题是当我尝试获取一行时,NHibernate 尝试在基表上选择 Field2 和 Field3,而应在 T1 表上选择它们。
我已经尝试了数十种黑客方法,但仍然不起作用,如果有人提出一个想法,那将非常有帮助。
多谢。
I'm using Nhibernate with Fluent and I encounter an issue with inheritance.
Here my DB schema
TABLE Base
- IDBASE (PK)
- Field1
- TYPE
TABLE T1
- IDBASE (PK and FK)
- Field2
- Field3 ...
My mapping files are :
public class BaseMap: ClassMap<BASE>
{
public BaseMap()
{
Id(x => x.Id, "IDBASE").GeneratedBy.Identity();
Map(x => x.Field1);
DiscriminateSubClassesOnColumn("TYPE");
}
}
public class T1Map: SubclassMap<T1>
{
public T1Map()
{
Table("T1");
KeyColumn("IDBASE");
DiscriminatorValue("T1");
Map(x => x.Field2).Not.Nullable();
Map(x => x.Field3).Not.Nullable();
}
}
I use FluentMappings instead of AutoMapping.
Here my entities :
public abstract class BASE
{
public virtual long IdBase{ get; set; }
public virtual string Field1 { get; set; }
}
public class T1: BASE
{
public virtual string Field2 { get; set; }
public virtual string Field3 { get; set; }
}
T1 entity inherits from BASE entity, the issue is when I try to get a row NHibernate try to select Field2 and Field3 on the Base Table whereas they should be selected on T1 Table.
I've tried dozens of hacks but it still doesn't work, if anyone as an idea it would be very helpful.
Thanks a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您指定了一个鉴别器,这意味着继承结构应该是每个类一个表的层次结构;通过此设置,您不会有两个表,您将拥有一个包含所有内容的表(因此选择的所有列都会命中同一个表)。
如果删除
DiscriminateSubclassesOnColumn
调用,则会将您的映射放入每个类的表中,这样您就可以获得所需的结构。You're specifying a discriminator, that implies that the inheritance structure should be a table-per-class-hierarchy; you won't have two tables with this setup, you'll have a single table with everything in (hence why the select is hitting the same table for all the columns).
If you remove the
DiscriminateSubclassesOnColumn
call, that'll put your mappings into a table-per-class, so you'll have your desired structure.实际上我真的需要 DiscriminateSubclassesOnColumn 所以我找到了另一个解决方案,在我继承的实体中我使用连接进行映射,如下所示:
它工作正常,我可以将 Discriminator 列保留在我的基类上。
谢谢
Actually I really need the
DiscriminateSubclassesOnColumn
so I found an other solution, in my inherited entity I do the mapping with the join like this :And it works fine, I can keep the Discriminator column on my base class.
Thanks