gpt4 book ai didi

c# - 删除抛出 "deleted object would be re-saved by cascade"

转载 作者:太空狗 更新时间:2023-10-29 21:48:10 24 4
gpt4 key购买 nike

我有以下模型:

<class name="Person" table="Person" optimistic-lock="version">
<id name="Id" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<!-- plus some properties here -->
</class>

<class name="Event" table="Event" optimistic-lock="version">
<id name="Id" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<!-- plus some properties here -->
</class>

<class name="PersonEventRegistration" table="PersonEventRegistration" optimistic-lock="version">
<id name="Id" type="Int32" unsaved-value="0">
<generator class="native" />
</id>
<property name="IsComplete" type="Boolean" not-null="true" />
<property name="RegistrationDate" type="DateTime" not-null="true" />
<many-to-one name="Person" class="Person" column="PersonId" foreign-key="FK_PersonEvent_PersonId" cascade="all-delete-orphan" />
<many-to-one name="Event" class="Event" column="EventId" foreign-key="FK_PersonEvent_EventId" cascade="all-delete-orphan" />
</class>

在 Person 和 Event 中都没有指向 PersonEventRegistration 的属性。

当我尝试从 PersonEventRegistration 中删除条目时,出现以下错误:

"deleted object would be re-saved by cascade"

问题是,我没有将此对象存储在任何其他集合中 - 删除代码如下所示:

public bool UnregisterFromEvent(Person person, Event entry)
{
var registrationEntry = this.session
.CreateCriteria<PersonEventRegistration>()
.Add(Restrictions.Eq("Person", person))
.Add(Restrictions.Eq("Event", entry))
.Add(Restrictions.Eq("IsComplete", false))
.UniqueResult<PersonEventRegistration>();

bool result = false;
if (null != registrationEntry)
{
using (ITransaction tx = this.session.BeginTransaction())
{
this.session.Delete(registrationEntry);
tx.Commit();
result = true;
}
}
return result;
}

我在这里做错了什么?

最佳答案

据我所知,cascade="all-delete-orphan" 属于集合映射元素,而不是多对一。您没有显示映射的其他两个部分,所以我不能肯定地说,但这可能(可能)是问题。

我认为 Person 应该是这样的:

<!-- other properties -->
<set name="Events" inverse="true" cascade="all-delete-orphan">
<key column="Person_id" />
<one-to-many class="PersonEventRegistration" />
</set>

事件:

<!-- similar mapping for Event -->

PersonEventRegistration:

<!-- other properties -->
<many-to-one name="Person" class="Person" column="PersonId" foreign-key="FK_PersonEvent_PersonId" cascade="delete" <!-- or many ="all" ? --> />

实际上,以上可能是相互冲突的级联(这可能是您所拥有的)。所以真的,我的答案是两件事:

  1. cascade="all-delete-orphan"many-to-one 没有意义。
  2. 确保您已经认真考虑过如何与您的实体合作以及它们应该如何级联它们的操作。

关于c# - 删除抛出 "deleted object would be re-saved by cascade",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/931129/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com