1
votes

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.

1

1 Answers

0
votes

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().