I have a class which is a child of 2 parents.
When I delete one of the parents, I expect the children to be deleted, but the other parent keeps them in the session with an error Deleted object would be resaved by cascade
class Company
{
// automap.HasMany(x=>x.Cars).Cascade.AllDeleteOrphan();
IList<Car> Cars { get; set; }
// automap.HasMany(x=>x.Employees).Cascade.AllDeleteOrphan();
IList<Employee> Employees { get; set; }
}
class Employee
{
// automap.references(x=>x.job);
Company job { get; set; }
// automap.hasMany(x=>x.OwnedCars).Cascade.AllDeleteOrphan();
IList<Cars> OwnedCars { get; set; }
}
class Car
{
// automap.references(x=>x.company)
Company company { get; set; }
// automap.references(x => x.Employee)
Employee Driver {get; set; } // might be null
}
When I remove an employee – i want his car to be removed from the company too;
var Company = Sesion..Get(1);
Company.Employees.removeAt(1);
Session.Update(Company);
Is there a better way to do the removal rather then traverse the list of cars, chech whether the car has belonged to the Employee I’m removing – and remove the car too before removing the employee ?
you will have to remove the association to the car from both the employee (maybe not necessary, as the employee will be deleted) and the company. So your delete would look like this: