仅适用于SaveOrUpdate和Merge的NHibernate级联约定

时间:2013-03-10 16:33:50

标签: .net nhibernate fluent-nhibernate nhibernate-mapping

我想为NHiberate配置一个约定,允许对SaveOrUpdate和Merge进行级联,但不允许删除。 我找不到如何做到这一点,因为Cascade.SaveOrUpdate()或Cascade.Merge()返回void。并且调用一种方法取消了先例。

以下是我的试验:

1)级联将用于SaveUpdate:

 public class CascadeAllConvention : IHasOneConvention, IHasManyConvention, IReferenceConvention
    {
        public void Apply(IOneToOneInstance instance)
        {
            instance.Cascade.Merge();
            instance.Cascade.SaveUpdate();
        }

        public void Apply(IOneToManyCollectionInstance instance)
        {
            instance.Cascade.Merge();
            instance.Cascade.SaveUpdate();

        }

        public void Apply(IManyToOneInstance instance)
        {
            instance.Cascade.Merge();
            instance.Cascade.SaveUpdate();
        }
    }

2)级联将用于合并:

 public class CascadeAllConvention : IHasOneConvention, IHasManyConvention, IReferenceConvention
    {
        public void Apply(IOneToOneInstance instance)
        {
            instance.Cascade.SaveUpdate();
            instance.Cascade.Merge();
        }

        public void Apply(IOneToManyCollectionInstance instance)
        {
            instance.Cascade.SaveUpdate();
            instance.Cascade.Merge();

        }

        public void Apply(IManyToOneInstance instance)
        {
            instance.Cascade.SaveUpdate();
            instance.Cascade.Merge();
        }
    }

3)级联将被停用:

 public class CascadeAllConvention : IHasOneConvention, IHasManyConvention, IReferenceConvention
    {
        public void Apply(IOneToOneInstance instance)
        {
            instance.Cascade.All();
            instance.Cascade.Not.Delete();
        }

        public void Apply(IOneToManyCollectionInstance instance)
        {
            instance.Cascade.All();
            instance.Cascade.Not.Delete();

        }

        public void Apply(IManyToOneInstance instance)
        {
            instance.Cascade.All();
            instance.Cascade.Not.Delete();
        }
    }

0 个答案:

没有答案