How to turn off cascade deleting?
質問
I have class with back reference:
public class Employee : Entity
{
private string _Name;
private string _Position;
private Employee _SupervisorBackRef;
private IList<Employee> _Subordinates;
private IList<BusinessPartner> _BusinessPartners;
public virtual string Name
{
get { return _Name; }
set { _Name = value; }
}
public virtual string Position
{
get { return _Position; }
set { _Position = value; }
}
public virtual Employee SupervisorBackRef
{
get { return _SupervisorBackRef; }
set { _SupervisorBackRef = value; }
}
public virtual IList<Employee> Subordinates
{
get { return _Subordinates; }
set { _Subordinates = value; }
}
public virtual IList<BusinessPartner> BusinessPartners
{
get { return _BusinessPartners; }
set { _BusinessPartners = value; }
}
}
Because back reference SupervisorBackRef and Subordinates share same foreign key:
create table Employees (
Id INT not null,
Name NVARCHAR(255) null,
Position NVARCHAR(255) null,
EmployeeFk INT null,
primary key (Id)
)
Problem is that although I tryed override it, if I delete any supervisor, it delete all his Subordinates. I tryed this:
class EmployeeOverride : IAutoMappingOverride<Employee>
{
public void Override(FluentNHibernate.Automapping.AutoMapping<Employee> mapping)
{
mapping.HasMany(x => x.Subordinates).Cascade.None();
mapping.HasOne(x => x.SupervisorBackRef).Cascade.None();
}
}
But it isn't work. I tryed it changed to different another combinations, i tryed this override delete, but nothing help.
If I delete any Employee, it delete all his Subordinates.
I don't know how can i recognize if this override function working.
From many texts on internet i understand that Cascade.None() should work, most people has opposite problem, deleting/updating/... don't work, so I'm really confused.
解決
Problem was, that I had in another place (in HasManyConvention.Apply
) set in instance Cascade.AllDeleteOrphan()
and it was higher priority.
PS: I don't know if is beter similar post like that answered od deleted. For me it looks like a problem which I had more time in past, but it is variation on "I dont't know my own code..."