nhibernate - 在更新父记录时复制新的子记录插入

发布于 2024-10-18 20:09:35 字数 2244 浏览 2 评论 0原文

类别和子类别具有一对多的关系。在类别更新模式下,我想删除所有现有的子类别并重新插入新的子类别(根据场景需要)。删除发生得很好,但是新的子记录正在重复(如果我有 2 个新的子记录,则操作是插入 2 + 2 条记录)

ISession session = NHibernateHelper.GetCurrentSession().GetSession(EntityMode.Poco);
        using (var tx = session.BeginTransaction())
        {
            List<Vtsubcategory> oovtsc = new List<Vtsubcategory>();
            oovtsc = oVtcategory.FkTocategory.Where(e => e.IdvtSubCategory != 0).ToList();
            foreach (Vtsubcategory ooVtsubcategory in oovtsc)
            {
                oVtcategory.FkTocategory.Remove(ooVtsubcategory);
                session.Delete(ooVtsubcategory);
            }
            session.SaveOrUpdateCopy(oVtcategory);
            session.Flush();
            if (tx.IsActive)
            {
                tx.Commit();
            }
        }

我猜它是在保存模式下插入子记录(对于子记录,因为它们是新的) ) 并在更新(父级)模式下再插入 2 个。不确定这是否属实以及如何解决。

映射是

<class name="Vtcategory" table="`vtcategory`" lazy="false">
<id name="IdvtCategory" column="`idvtCategory`" type="int">
  <generator class="native" />
</id>
<property type="string" length="100" name="Catname" column="`catname`" />
<property type="string" length="45" name="Catshortname" column="`catshortname`" />
<property type="DateTime" name="Crdt" column="`crdt`" />
<property type="string" length="45" name="Crby" column="`crby`" />
<bag name="FkTocategory" inverse="false" lazy="true" cascade="all">
  <key column="`catid`" />
  <one-to-many class="AMSDAL.Vtsubcategory,AMSDAL" />
</bag>

  <class name="Vtsubcategory" table="`vtsubcategory`" lazy="false">
<id name="IdvtSubCategory" column="`idvtSubCategory`" type="int">
  <generator class="native" />
</id>
<property type="string" length="100" name="Subcatname" column="`subcatname`" />
<property type="string" length="45" name="Subcatshortname" column="`subcatshortname`" />
<property type="DateTime" name="Crdt" column="`crdt`" />
<property type="string" length="45" name="Crby" column="`crby`" />
<many-to-one name="Catid" cascade="none" column="`catid`" />

Category and Subcategory have 1 to Many relationship. In update mode for category, I want to delete all existing subcategories and re-insert the new one (as scenario demands it). Deletion is happening fine, however the new child records are getting duplicated (If I've 2 new records for child, operation is inserting 2 + 2 records)

ISession session = NHibernateHelper.GetCurrentSession().GetSession(EntityMode.Poco);
        using (var tx = session.BeginTransaction())
        {
            List<Vtsubcategory> oovtsc = new List<Vtsubcategory>();
            oovtsc = oVtcategory.FkTocategory.Where(e => e.IdvtSubCategory != 0).ToList();
            foreach (Vtsubcategory ooVtsubcategory in oovtsc)
            {
                oVtcategory.FkTocategory.Remove(ooVtsubcategory);
                session.Delete(ooVtsubcategory);
            }
            session.SaveOrUpdateCopy(oVtcategory);
            session.Flush();
            if (tx.IsActive)
            {
                tx.Commit();
            }
        }

I guess it is inserting the child records in Save mode (for Child as they are new) and Inserting 2 more in Update (Parent) mode. Not sure if this is true and how to address it.

mapping is

<class name="Vtcategory" table="`vtcategory`" lazy="false">
<id name="IdvtCategory" column="`idvtCategory`" type="int">
  <generator class="native" />
</id>
<property type="string" length="100" name="Catname" column="`catname`" />
<property type="string" length="45" name="Catshortname" column="`catshortname`" />
<property type="DateTime" name="Crdt" column="`crdt`" />
<property type="string" length="45" name="Crby" column="`crby`" />
<bag name="FkTocategory" inverse="false" lazy="true" cascade="all">
  <key column="`catid`" />
  <one-to-many class="AMSDAL.Vtsubcategory,AMSDAL" />
</bag>

  <class name="Vtsubcategory" table="`vtsubcategory`" lazy="false">
<id name="IdvtSubCategory" column="`idvtSubCategory`" type="int">
  <generator class="native" />
</id>
<property type="string" length="100" name="Subcatname" column="`subcatname`" />
<property type="string" length="45" name="Subcatshortname" column="`subcatshortname`" />
<property type="DateTime" name="Crdt" column="`crdt`" />
<property type="string" length="45" name="Crby" column="`crby`" />
<many-to-one name="Catid" cascade="none" column="`catid`" />

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

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

发布评论

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

评论(2

梦在深巷 2024-10-25 20:09:35

您需要将关系的一侧标记为逆关系。这通常是一对多的一侧:

<bag name="FkTocategory" inverse="true" lazy="true" cascade="all">
    <key column="`catid`" />
    <one-to-many class="AMSDAL.Vtsubcategory,AMSDAL" />
</bag>

You need to mark one side of the relationship as the inverse. This is typically the one side on a one-to-many:

<bag name="FkTocategory" inverse="true" lazy="true" cascade="all">
    <key column="`catid`" />
    <one-to-many class="AMSDAL.Vtsubcategory,AMSDAL" />
</bag>
绝不放开 2024-10-25 20:09:35

将包上的级联标记为 all-delete-orphan 而不是 all。然后,在代码中,您只需将包含新子类别的新列表分配给 FkToCategory 属性即可。这将确保 NHibernate 自动删除所有孤立的子类别。

Mark the cascade on the bag to be all-delete-orphan rather than all. Then in your code you can just assign the new list with the new subcategories to the FkToCategory property. This will ensure that all orphaned subcategories will be deleted automatically by NHibernate.

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