2012-06-06 25 views
0

我有一個班是2個家長的孩子。 當我刪除一個家長,我希望孩子們被刪除,但其他家長有錯誤Deleted object would be resaved by cascade流利nhibernate兩個集合中的互惠兒童

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 
} 

讓他們在會話當我刪除僱員 - 我想除去他的車來自公司;

var Company = Sesion..Get(1); 
Company.Employees.removeAt(1); 
Session.Update(Company); 

有沒有更好的辦法做到去除而不是穿過汽車的名單,再檢查一下車子是否已經屬於我消除員工 - 和消除員工之前卸下車嗎?

回答

1

您必須從員工(可能不需要,因爲該員工將被刪除)和公司中刪除與汽車的關聯。所以你的刪除看起來像這樣:

var company = session.Get(1); 
var employee = company.Employees[1]; 
foreach(var car in employee.OwnedCars) 
{ 
    car.company.Cars.Remove(car); 
} 
company.Employees.Remove(employee); 
session.Update(company);