0

我有带回参考的课:

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; }
        }
    }

因为反向引用 SupervisorBackRef 和 Subordinates 共享相同的外键:

create table Employees (
        Id INT not null,
       Name NVARCHAR(255) null,
       Position NVARCHAR(255) null,
       EmployeeFk INT null,
       primary key (Id)
    )

问题是虽然我尝试覆盖它,但如果我删除任何主管,它会删除他的所有下属。我试过这个:

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();
    }
}

但这行不通。我尝试将其更改为不同的其他组合,我尝试了此覆盖删除,但没有任何帮助。

如果我删除任何员工,它会删除他的所有下属。

我不知道如何识别此覆盖功能是否有效。

从互联网上的许多文本中,我了解到 Cascade.None() 应该可以工作,大多数人都有相反的问题,删除/更新/...不起作用,所以我真的很困惑。

4

1 回答 1

2

问题是,我在另一个地方(in HasManyConvention.Apply)设置了实例Cascade.AllDeleteOrphan(),它的优先级更高。

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..."

于 2012-06-16T07:55:48.700 回答