Fluent NHibernate 子类参考

Fluent NHibernate subclass reference

所以我有以下实体:

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、Demand 和 ServiceDemandConnection 使用 ClassMap 进行映射 带有 Id(x => x.Id)。 ServiceRecord 使用 SubclassMap (table-per-class) 进行映射。 ServiceDemandConnection 中的引用使用 References(x => x.Service).Cascade.None() 进行映射,对于 .Demand.

也是如此

问题在于插入 ServiceDemandConnection 时正确设置了 ServiceRecord 和 Demand。 我得到一个错误:Detail=Key (servicerecord_id)=(8) is not present in table "ServiceRecord"。哪些错误陈述是正确的。 8 是 TimelineRecord 的 ID,不是 ServiceRecord。但是,应该使用 ServiceRecord 的 ID(TimelineRecord_ID,实际上在代码中 mapped/not 无法访问)。当前映射隐藏 ServiceRecord.ID.

我应该如何告诉 NHibernate 使用子 class table (ServiceRecord) 的 ID,而不是基础 class table (TimelineRecord) 的 ID? NHibernate 实际上在数据库中创建了一个适当的约束,但在运行时它以某种方式违反了它。

您需要

  • map ServiceRecord as separate class not using SubclassMap to use it's ID
  • 或使用基础 class 的 ID,不要将其映射到 SubclassMap

第二种方法有效,因为 SubclassMap 在子对象和父对象之间创建了 FK 关系,这样数据是一致的,你有类似

的东西
TimeLineRecord
  ID : PK

ServiceRecord extends TimelineRecord:
  TimelineRecord_ID: FK -----> TimeLineRecord.ID

指向子 classess 的引用仍然有效。