如何使用 FluentNHibernate 删除引用的对象(老话“删除的对象将通过级联重新保存”)
我遇到的错误很常见,但我还没有找到任何适合我的场景的答案:
实体:
学校 老师 学生
映射:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的其余映射是什么样子的?学校和学生之间的逆向设置是什么?
尝试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()
.