流利的Nhibernate - HasMany - 未保存子对象

时间:2011-07-13 14:20:01

标签: nhibernate fluent-nhibernate

我将ReportRequest对象定义为:

public class ReportRequest
{
    public virtual Int32? Id { get; set; }
    public virtual string Description { get; set; }
    public virtual IList<ReportClient> ReportClients{get;set;}
}

和ReportClient定义为:

public class ReportClient
{
    public virtual int? Id { get; set; }

    public virtual long? ClientId { get; set; }

    public virtual string Name { get; set; }

    public virtual string EmailAddress { get; set; }

    public virtual IList<ReportClient> ChildClients { get; set; }
}

ReportClient的映射:

public class ReportClientMap : ClassMap<ReportClient>
{
    public ReportClientMap()
    {
        Id(x => x.Id).UnsavedValue(null).GeneratedBy.Native();
        Map(x => x.ClientId);
        Map(x => x.Name);
        Map(x => x.EmailAddress).Length(255);
        HasMany<ReportClient>(x => x.ChildClients)
            .KeyColumn("ParentId")
            .KeyNullable()
            .AsBag()
            .Inverse()
            .ForeignKeyConstraintName("FK_ReportClient_ParentId");
    }
}

ChildClients需要保存为ReportClient,但设置了ParentId。

虽然ReportRequest和ReportClients保存得很好,但我面临的问题是ReportClient.ChildClients没有被保存。

我甚至没有收到任何错误。

有什么想法吗?

1 个答案:

答案 0 :(得分:7)

您需要为ChildClients属性映射设置级联到保存更新:

  HasMany<ReportClient>(x => x.ChildClients)
        .KeyColumn("ParentId")
        .KeyNullable()
        .AsBag()
        .Inverse()
        .Cascade.SaveUpdate()
        .ForeignKeyConstraintName("FK_ReportClient_ParentId");