因此,我已以下实体:功能NHibernate子类参考
TimelineRecord:
ID: PK
From: DateTime
To: DateTime
ServiceRecord extends TimelineRecord:
ID: PK
TimelineRecord_ID: FK
SomeSpecificProperties...
Demand:
ID: PK
From: DateTime
To: DateTime
...
ServiceDemandConnection:
ID: PK
Service: ServiceRecord
Demand: Demand
TimelineRecord,需求和ServiceDemandConnection使用类映射 与标识映射(X => x.Id)。 ServiceRecord使用SubclassMap(table-per-class)进行映射。 ServiceDemandConnection中的引用使用引用(x => x.Service).Cascade.None()映射,对于.Demand也是如此。
问题是插入ServiceDemandConnection并正确设置了ServiceRecord和Demand。 我收到一个错误:Detail = Key(servicerecord_id)=(8)在表“ServiceRecord”中不存在。什么错误状态是真实的。 8是TimelineRecord的ID,而不是ServiceRecord。但是,应该使用ServiceRecord的ID(TimelineRecord_ID,实际上未映射/代码中无法访问)。当前映射隐藏ServiceRecord.ID。
我该如何告诉NHibernate使用子类表(ServiceRecord)的ID,而不是基类表(TimelineRecord)? NHibernate实际上在数据库中创建了一个适当的约束,但是在运行时却以某种方式违反了它。
我想保留继承。第二种选择是可能的,但是我不会在DB中有适当的约束。任何其他想法? – wysek 2015-04-01 14:16:19
是的,你会的。带有第二个选项的ID只存在于基类中,而子类仅将FK保留为父类。在两个级别上保留id是多余的,这可能会导致问题 – tchrikch 2015-04-01 14:17:27
在此上下文中,_proper constraint_我的意思是对ServiceDemandConnection.Service FK的约束。使用第二种方法,我需要将ServiceDemandConnection.Service的类型从ServiceRecord更改为TimelineRecord,并检查其在业务逻辑中的有效性。我错过了什么吗? – wysek 2015-04-01 14:37:16