nhibernate - 在更新父记录时复制新的子记录插入
类别和子类别具有一对多的关系。在类别更新模式下,我想删除所有现有的子类别并重新插入新的子类别(根据场景需要)。删除发生得很好,但是新的子记录正在重复(如果我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将关系的一侧标记为逆关系。这通常是一对多的一侧:
You need to mark one side of the relationship as the inverse. This is typically the one side on a one-to-many:
将包上的级联标记为
all-delete-orphan
而不是all
。然后,在代码中,您只需将包含新子类别的新列表分配给 FkToCategory 属性即可。这将确保 NHibernate 自动删除所有孤立的子类别。Mark the cascade on the bag to be
all-delete-orphan
rather thanall
. 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.