Fluent Nhibernate - HasMany - 子对象未保存

发布于 2024-11-19 17:53:22 字数 1349 浏览 1 评论 0原文

我有一个 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");
    }
}

需要将 ChildClient 保存为 ReportClient 但设置了 ParentId。

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

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

有什么想法吗?

I have a ReportRequest object defined as:

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

and ReportClient is defined as:

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; }
}

mapping for 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 are required to be saved as ReportClient but with ParentId being set.

Though ReportRequest and ReportClients are being saved fine, but the issue I am facing is ReportClient.ChildClients are not being saved.

I am not even getting any error.

Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为你拒绝所有暧昧 2024-11-26 17:53:22

您需要将层叠设置为 ChildClients 属性映射的 save-update :

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

You need to set cascade to save-update for ChildClients property mapping:

  HasMany<ReportClient>(x => x.ChildClients)
        .KeyColumn("ParentId")
        .KeyNullable()
        .AsBag()
        .Inverse()
        .Cascade.SaveUpdate()
        .ForeignKeyConstraintName("FK_ReportClient_ParentId");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文