How to delete a reference object using FluentNHibernate (the old "olde" object will be cascaded ")

The error I am getting is common, but in the script I did not find the answers that say in my script:

Entities:

School Teacher Student

Mapping:

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: A student is associated with a School in which there are no other students or teachers. If I want to connect a student with another school, I would like to remove the orphan school.

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

What happens is that when I go to save the โ€œstudentโ€, I get the error โ€œthe deleted object will be cascadedโ€.

As always, any help is greatly appreciated.

+1
source share
1 answer

What do the rest of your comparisons look like? What is the feedback between School and Student?

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

0
source

All Articles