我的情况类似于Fluent NHibernate Mapping not on PK Field中描述的情况
但是,我的表之间的关系是由多个非主键列描述的。想象一下 Chris Meek 的情况,但是 aPerson
有 aJobType
和 a应该Code
(对不起,它是一个遗留数据库)唯一地描述Person
Person
------
Id PK
JobType
Code
Name
Order
-----
Id PK
Person_JobType
Person_Code
OrderDetails
Serhat Özgel 的回答描述了使用PropertyRef
,但我找不到为多个列单独执行此操作的方法。我试过类似于
class PersonMap : ClassMap<Person>
{
public PersonMap()
{
HasMany(p => p.Order)
.KeyColumns.Add("Person_JobType")
.PropertyRef("JobType")
.KeyColumns.Add("Person_Code")
.PropertyRef("Code")
}
}
但这显然不起作用,因为KeyColumns.Add()
返回另一个OneToManyPart
所以PropertyRef()
不会针对正在添加的单个列运行。第二个PropertyRef()
只是覆盖第一个,我收到以下错误:
NHibernate.MappingException : collection foreign key mapping
has wrong number of columns: MyApp.Person.Order type: Int32
我查看了 KeyColumns.Add() 的各种重载,
public TParent Add(string name)
public TParent Add(params string[] names)
public TParent Add(string columnName, Action<ColumnPart> customColumnMapping)
public TParent Add(ColumnMapping column)
特别是最后两个,但找不到任何方法PropertyRef
为每列单独设置级别:(有没有办法做到这一点?我是否完全以错误的方式去做?