Fluent NHibernate子集合持久性问题

Fluent NHibernate子集合持久性问题,nhibernate,fluent-nhibernate,Nhibernate,Fluent Nhibernate,我有以下映射类(仅复制了相关部分): 这不会保存新的CostStructure实例。为什么? 第二个:假设我加载了一个CardTemplate,它有一个CostStructure包含三个CostStructureComponent实体。现在我想从CostStructure中删除其中一个CostStructureComponents 我尝试了以下方法: costStructure.CostComponents.Remove(component); component.CostStructure =

我有以下映射类(仅复制了相关部分):

这不会保存新的
CostStructure
实例。为什么?

第二个:假设我加载了一个
CardTemplate
,它有一个
CostStructure
包含三个
CostStructureComponent
实体。现在我想从
CostStructure
中删除其中一个
CostStructureComponents

我尝试了以下方法:

costStructure.CostComponents.Remove(component);
component.CostStructure = null;
session.Save(template)
现在,我知道显式删除是有效的,但是
deleteonforn
部分不也应该声明现在没有
CostStructure
引用的组件被删除了吗?相反,NHibernate尝试执行以下更新:

UPDATE CostStructureComponent 
SET amount = @p0, 
     coststructure_id = @p1, 
     resourcetype_id = @p2
WHERE id = @p3;

@p0 = 1 [Type: Int32 (0)], 
@p1 = NULL [Type: Int64 (0)], 
@p2 = 5 [Type: Int64 (0)], 
@p3 = 13 [Type: Int64 (0)]
Equals/GetHashCode是否以错误的方式实现


对不起,已经很晚了,又是漫长的一天等等……如果我说的是胡言乱语,请告诉我……

所有答案都隐藏在一个设置中
.Inverse()

在这里:

public CostStructureMapping()
{
    ..
    HasMany(x => x.CostComponents)
        .KeyColumn("coststructure_id")
        .Cascade.AllDeleteOrphan()
        .Inverse();  // and HERE and everywhere on HasMany()
尝试查找一些关于此设置的文章,但一般来说:如果映射为反向,NHibernate准备好做更好的SQL语句,因为它正在处理关系的另一端

一些资料来源:

引用:

然而,在设计良好的NHibernate域模型中,我们通常会看到大多数集合实际上是一对多的关联。对于这些关联,更新由关联的
多对一
(注释:Fluent中的References)端处理,因此集合更新性能的考虑不适用

反向(可选-默认为false)将此集合标记为双向关联的“反向”端


谢谢,拉迪姆,我回家后会试试这个。
UPDATE CostStructureComponent 
SET amount = @p0, 
     coststructure_id = @p1, 
     resourcetype_id = @p2
WHERE id = @p3;

@p0 = 1 [Type: Int32 (0)], 
@p1 = NULL [Type: Int64 (0)], 
@p2 = 5 [Type: Int64 (0)], 
@p3 = 13 [Type: Int64 (0)]
public CardTemplateMapping()
{
    HasMany(x => x.CostStructures)
        .KeyColumn("cardtemplate_id")
        .Cascade.All()
        .Inverse(); // HERE this setting
public CostStructureMapping()
{
    ..
    HasMany(x => x.CostComponents)
        .KeyColumn("coststructure_id")
        .Cascade.AllDeleteOrphan()
        .Inverse();  // and HERE and everywhere on HasMany()