[英]Fluent NHibernate Subclass mapping with foreign key
我有以下对象:
class Person { Int32 id; String name; /*..*/ Adress adress; }
class Employee : Person { String e_g_Tax; /*..*/ Guid relationshipToManagmentId; }
以及以下映射前提:
(a)“ relationshipToManagmentId”应为外键。
(b)表“ RelationshipToManagment”是一个非映射表,(应用程序的旧部分)
(c)映射策略是TPT。 (至少对于新对象:-)
映射,直到现在:
public class PersonMap : ClassMap<Person> {
public PersonMap(){
Id(x => x.id);
Map (x => x.Nachname).Length(255).Not.Nullable();
/*..*/
References(x => x.Adresse).Class(typeof(Adresse)).Not.Nullable();
}
}
public class EmployeeMap : SubclassMap<Employee>
{
public EmployeeMap()
{
Map(x => x.e_g_Tax, "enjoytax")
.Not.Nullable();
/*..*/
Join("RelationshipToManagment", xJoin =>
{
//xJoin.Table("RelationshipToManagment");
xJoin.Fetch.Join();
xJoin.KeyColumn("ID");
xJoin.Map(x => x.relationshipToManagmentId)
.Not.Nullable() ;
}); // --> exception!!
我该怎么写?
Join()
只能在主键(属性ID)上Join()
到另一个表,但是您需要在外键列上Join()
。 普通参考可以满足您的需求
class Employee : Person
{
Management Management;
}
public EmployeeMap()
{
References(x => x.Management).Column("relationshipToManagmentId");
}
更新:如果您需要从RelationshipToManagment
表中获得只读信息,则可以使用公式属性
public EmployeeMap()
{
Map(x => x.RelationshipToManagment).Formula("(SELECT m.Title FROM RelationshipToManagment m WHERE m.Id = relationshipToManagmentId)");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.