当将新项目插入非空集合时,NHibernate 级联集合删除

发布于 2024-09-29 21:30:04 字数 936 浏览 2 评论 0原文

我有以下流畅的映射:

    public ScanDeliverySessionMap()
    {
        Id(x => x.Id);
        ...
        ...
        HasManyToMany(x => x.ToScanForms) <--- IList<Form> ToScanForms --->
            .Table("ToScanForm")
            .ParentKeyColumn("SessionId")
            .ChildKeyColumn("FormId").Cascade.SaveUpdate();
    }

    public FormMap()
    {

        Id(x => x.Id).Column("FormID").GeneratedBy.Foreign("Log");

        ....
        ....

        HasManyToMany(x => x.ScanDeliverySessions)
            .Table("ToScanForm")
            .ParentKeyColumn("FormId")
            .ChildKeyColumn("SessionId").Inverse();
    }

当我尝试将新表单插入 ToScanForms 集合时 一切看似正常,但在 NProf 上观看 我看到 NH casacde DELETE 删除了所有 ToScanForms 项目 然后 NH INSERT ToScanForms 项目(包括新项目)。

一些截图: 替代文字 替代文本

I have the following Fluent Mappings:

    public ScanDeliverySessionMap()
    {
        Id(x => x.Id);
        ...
        ...
        HasManyToMany(x => x.ToScanForms) <--- IList<Form> ToScanForms --->
            .Table("ToScanForm")
            .ParentKeyColumn("SessionId")
            .ChildKeyColumn("FormId").Cascade.SaveUpdate();
    }

    public FormMap()
    {

        Id(x => x.Id).Column("FormID").GeneratedBy.Foreign("Log");

        ....
        ....

        HasManyToMany(x => x.ScanDeliverySessions)
            .Table("ToScanForm")
            .ParentKeyColumn("FormId")
            .ChildKeyColumn("SessionId").Inverse();
    }

When I try to insert new Form to the ToScanForms collection
Everything seemingly works properly but watching on NHProf
I see that NH casacde DELETE over all ToScanForms items
and then NH INSERT the ToScanForms items including the new item.

Some screenshots:
alt text
alt text

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

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

发布评论

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

评论(1

吻安 2024-10-06 21:30:04

发生这种行为是因为 nhibernate 不知道集合中哪些实体是新的、哪些是旧的,因此他必须删除所有内容,然后重新插入它们。

要防止这种行为非常简单:将属性更改为 ICollection 并将 HasManyToMany 映射为集合。您的映射将更改为以下内容:

public ScanDeliverySessionMap()
{
    Id(x => x.Id);
    ...
    ...
    HasManyToMany(x => x.ToScanForms) //<--- ICollection<Form> ToScanForms --->
        .AsSet()
        .Table("ToScanForm")
        .ParentKeyColumn("SessionId")
        .ChildKeyColumn("FormId").Cascade.SaveUpdate();
}

public FormMap()
{

    Id(x => x.Id).Column("FormID").GeneratedBy.Foreign("Log");

    ....
    ....

    HasManyToMany(x => x.ScanDeliverySessions)
        .AsSet()
        .Table("ToScanForm")
        .ParentKeyColumn("FormId")
        .ChildKeyColumn("SessionId").Inverse();
}

在底层,nhibernate 将使用 Iesi Collections 的 HashSet,因此现在他知道哪些实体是新的,哪些是旧的。

That behaviour occurs because nhibernate doesn't know which entities in the collection are new and which are old, so he must delete everything and then re-insert them.

To prevent this is behaviour is quite simple: change your property to an ICollection and map your HasManyToMany as a set. You mapping would be changed to the following:

public ScanDeliverySessionMap()
{
    Id(x => x.Id);
    ...
    ...
    HasManyToMany(x => x.ToScanForms) //<--- ICollection<Form> ToScanForms --->
        .AsSet()
        .Table("ToScanForm")
        .ParentKeyColumn("SessionId")
        .ChildKeyColumn("FormId").Cascade.SaveUpdate();
}

public FormMap()
{

    Id(x => x.Id).Column("FormID").GeneratedBy.Foreign("Log");

    ....
    ....

    HasManyToMany(x => x.ScanDeliverySessions)
        .AsSet()
        .Table("ToScanForm")
        .ParentKeyColumn("FormId")
        .ChildKeyColumn("SessionId").Inverse();
}

Under the hood nhibernate will use Iesi Collections' HashSet, so now he knows which entities are new and which are old.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文