How to delete a referenced object using FluentNHibernate (ye olde "deleted object would be resaved by cascade")
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
Ma开发者_JAVA技巧ppings:
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.
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()
.
精彩评论