在您的情况下,我会执行以下操作:
public class HardwareDevice{
public virtual int Id {get; set;}
public virtual string SerialNumber {get; set;}
//Other stuff
}
public class DomainThingA {
public virtual int Id {get; set;}
public virtual HardwareDevice Device {get; set;}
//Other stuff
}
public class DomainThingB {
public virtual int Id {get; set;}
public virtual HardwareDevice Device {get; set;}
//Other stuff
}
使用 AutoGenerated Id 作为主键映射您的 HardwareDevice 类。我的示例使用 FluentNhibernate 作为类映射。
public class HardwareDeviceMap : ClassMap<HardwareDevice> {
public HardwareDeviceMap(){
Id(x=>x.Id).GeneratedBy.Native().Column("Id"); //Uses auto number
Map(x=>x.SerialNumber).Column("SerialNumber");
//Other mappings
}
}
现在映射出其他两个类:
public class DomainThingAMap : ClassMap<DomainThingA> {
public DomainThingAMap(){
Id(x=>x.Id).GeneratedBy.Native(); //Uses auto number
References(x=>x.Device)
.Column("DeviceId"); //Joins on Id in HardwareDevice Table by default
//Other mappings
}
}
public class DomainThingBMap : ClassMap<DomainThingB> {
public DomainThingBMap(){
Id(x=>x.Id).GeneratedBy.Native(); //Uses auto number
References(x=>x.Device)
.Column("SerialNumber") //Column in DomainThingB Table
.PropertyRef("SerialNumber"); //Joins using SerialNumber column (hardware device table)
//Other mappings
}
}
类映射的 Property-Ref 特性允许您连接不是这些类型的遗留数据库的主键的列。