我有以下课程:
public class Customer {
public virtual int Id {get; set;}
public virtual string Surname {get; set;}
public virtual string Prename {get; set;}
public virtual Location Location {get; set;}
}
public class Location {
public virtual int Id {get; set;}
public virtual string ZipCode {get; set;}
public virtual string Name {get; set;}
}
public class CustomLocation : Location {
}
以及以下映射:
public class CustomerMapping : ClassMapping<Customer> {
public CustomerMapping(){
Table("Customer");
Property(a => a.Surname, b =>
{
b.NotNullable(true);
});
Property(a => a.Prename, b =>
{
b.NotNullable(true);
});
ManyToOne(a => a.Location, b =>
{
b.Column($"FK_Location_Id");
});
}}
public class LocationMapping : ClassMapping<Location> {
public LocationMapping(){
Table("Location");
Property(a => a.ZipCode, b =>
{
b.NotNullable(true);
});
Property(a => a.Name, b =>
{
b.NotNullable(true);
});
}}
public class CustomLocationMapping : ClassMapping<CustomLocation>{
public CustomLocationMapping(){
Table("CustomLocation");
Property(a => a.ZipCode, b =>
{
b.NotNullable(true);
});
Property(a => a.Name, b =>
{
b.NotNullable(true);
});
}}
我的目标是我有一个Location
表,该表由脚本自动更新,还有一个CustomLocation
表,如果有一些丢失的地方(或在国外),用户可以在其中添加位置。
我的问题是,我不知道如何正确地将其映射到Customer
或Location
的{{1}}。
有什么想法吗?预先感谢。
答案 0 :(得分:0)
您不能将一个属性映射到两个不同表中的两列。 NHibernate怎么知道要使用哪个表?您还将失去该列的参照完整性。
但是,您可以通过使用略有不同的方法来实现您的目标:不必对同一结构使用两个表,而只使用一个具有以下类表示形式的Location
-表:
public class Location
{
public virtual int Id { get; set; }
public virtual string ZipCode { get; set; }
public virtual string Name { get; set; }
public virtual bool IsSystem { get; set; } // just use a boolean column in the table for this one
}
您的脚本可以插入/更新/删除IsSystem == true
的所有行,而忽略其余的行。用户添加条目时,只需设置IsSystem = false
。