如何使用 FluentNHibernate 删除引用的对象(老话“删除的对象将通过级联重新保存”)

发布于 2024-12-07 01:27:57 字数 871 浏览 0 评论 0原文

我遇到的错误很常见,但我还没有找到任何适合我的场景的答案:

实体:

学校 老师 学生

映射:

School:  mapping.HasMany(x => x.Students).Cascade.AllDeleteOrphan();
Student: 
      mapping.References(x => x.Teacher).Not.Nullable().Cascade.SaveUpdate();
      mapping.References(x => x.School).Not.Nullable().Cascade.SaveUpdate();
Teacher: 
      mapping.References(x => x.School).Not.Nullable().Cascade.SaveUpdate();
      mapping.HasMany(x => x.Students).Cascade.All().Inverse();

场景:学生链接到没有其他学生或教师的学校。如果我想将学生链接到另一所学校,我想删除孤立的学校。

if (oldSchool.Students.Count == 1 && oldSchool.Teachers.Count == 0)
{
    //delete it
    //oldSchool.Students.Remove(student);
    student.School = null;

    _schoolRepository.Delete(oldSchool);
}

这里发生的情况是,当我去保存“学生”时,我收到可怕的“删除的对象将通过级联重新保存”错误。

一如既往,我们非常感谢任何帮助。

The error I'm getting is common, but the scenario I haven't found any answers that speak to my scenario:

Entities:

School
Teacher
Student

Mappings:

School:  mapping.HasMany(x => x.Students).Cascade.AllDeleteOrphan();
Student: 
      mapping.References(x => x.Teacher).Not.Nullable().Cascade.SaveUpdate();
      mapping.References(x => x.School).Not.Nullable().Cascade.SaveUpdate();
Teacher: 
      mapping.References(x => x.School).Not.Nullable().Cascade.SaveUpdate();
      mapping.HasMany(x => x.Students).Cascade.All().Inverse();

Scenario: Student is linked to a School that has no other Students or Teachers. If I want to link the student to a different school, I'd like to delete the orphaned school.

if (oldSchool.Students.Count == 1 && oldSchool.Teachers.Count == 0)
{
    //delete it
    //oldSchool.Students.Remove(student);
    student.School = null;

    _schoolRepository.Delete(oldSchool);
}

What happens here is that, when I go to save "student", I get the dreaded "deleted object would be resaved by cascade" error.

As always, any help greatly appreciated.

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

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

发布评论

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

评论(1

羁绊已千年 2024-12-14 01:27:57

您的其余映射是什么样子的?学校和学生之间的逆向设置是什么?

尝试mapping.HasMany(x => x.Students).Cascade.AllDeleteOrphan().Inverse()。

What's the rest of your mappings look like? What's the inverse setting between the School and the Student?

Try mapping.HasMany(x => x.Students).Cascade.AllDeleteOrphan().Inverse().

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