1

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.

sydneyos
  • 4,527
  • 6
  • 36
  • 53

1 Answers1

0

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

rbellamy
  • 5,683
  • 6
  • 38
  • 48
  • That's the totality of the mappings involving these classes. I'm not clear where the Inverse goes in a References scenario vs a HasMany/HasOne scenario. Will try that. – sydneyos Sep 28 '11 at 23:48
  • Actually, this is where I got tripped up - References doesn't have a Cascade.AllDeleteOrphan option, nor does it have and Inverse() option. – sydneyos Sep 29 '11 at 04:52
  • Right, I should have checked my psuedo code before I posted - You don't generally place an Inverse on the References end - it should go on the `mapping.HasMany(x => x.Students)` side. As for the AllDeleteOrphan you've got that correct from what I can see. One thing you can do is emit the actual mapping files, tweak them until they work, and then figure out how to emit the tweaked files with FNH. I've updated my answer to reflect this comment. – rbellamy Sep 29 '11 at 13:58
  • Unfortunately, this doesn't work - it is still saying that the deleted object would be resaved by cascade. – sydneyos Sep 29 '11 at 16:21
  • Then you need to come at it from the mappings - make those work the way you want, and then determine the best way to get FNH to do it for you. – rbellamy Sep 29 '11 at 21:03