Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fluent NHibernate子类引用_Nhibernate_Fluent Nhibernate_Subclass_Nhibernate Mapping - Fatal编程技术网

Fluent NHibernate子类引用

Fluent NHibernate子类引用,nhibernate,fluent-nhibernate,subclass,nhibernate-mapping,Nhibernate,Fluent Nhibernate,Subclass,Nhibernate Mapping,因此,我有以下实体: TimelineRecord: ID: PK From: DateTime To: DateTime ServiceRecord extends TimelineRecord: ID: PK TimelineRecord_ID: FK SomeSpecificProperties... Demand: ID: PK From: DateTime To: DateTime ... Servi

因此,我有以下实体:

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使用类映射进行映射 使用Id(x=>x.Id)。ServiceRecord使用子类映射(每个类的表)进行映射。 ServiceDemandConnection中的引用使用引用(x=>x.Service).Cascade.None()和.Demand中的引用进行映射

问题在于插入ServiceDemandConnection时正确设置了ServiceRecord和Demand。 我得到一个错误:表“servicerecord”中不存在Detail=Key(servicerecord_id)=(8)。错误说明的是正确的。8是TimelineRecord的ID,而不是ServiceRecord。但是,应该使用ServiceRecord的ID(TimelineRecord_ID,它实际上在代码中没有映射/不可访问)。当前映射隐藏ServiceRecord.ID

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

您需要

  • 映射
    servicecord
    作为单独的类,不使用
    子类映射
    来使用它的
    ID
  • 或者从基类中使用
    ID
    ,不要在
    子类映射中映射它
第二种方法之所以有效,是因为
子类映射
在子对象和父对象之间创建了一个
FK
关系,这样数据就一致了,并且

TimeLineRecord
  ID : PK

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

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

我想保留继承。第二个选项是可能的,但是我在DB中没有适当的约束。还有其他想法吗?是的,你会的。带有第二个选项的ID仅存在于基类中,而子类仅维护FK到父类。在两个级别上保留id是多余的,这可能会导致问题。在本文中,通过适当的约束,我指的是对ServiceDemandConnection.Service FK的约束。使用第二种方法,我需要将ServiceDemandConnection.Service的类型从ServiceRecord更改为TimelineRecord,并在业务逻辑中检查其有效性。我遗漏了什么吗?我误解了你关于在两个级别上保持ID的暗示。这是多余的。第二个解决方案对我有效。