软删除Nhibernate

时间:2012-09-06 08:34:40

标签: c# nhibernate nhibernate-3 soft-delete

我想在我的数据库表上进行软删除...我已经应用了以下语句(如此处http://nhibernate.info/blog/2008/09/06/soft-deletes.html所述,并且在很多问题上都是如此)。 Fattura是我的表格,我想要应用逻辑删除(没有触发器)

Fattura.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Paggentola.Gestionale.DL.Model"   namespace="Paggentola.Gestionale.DL.Model">
  <class name="Fattura" table="Fattura"  where="Cancellato=0">
    <id name="Id_Fattura" column="Id_Fattura">
      <generator class="native" />
    </id>
    <property name="Tipo_Fattura" column="Tipo_Fattura" />
    <property name="Cancellato" column="Cancellato" />
  </class>
</hibernate-mapping>

Fattura.cs - 模型对象

using System;

namespace ModelObject
{
    public class Fattura : ISoftDeletable
    {
        public virtual int Id_Fattura { get; set; }     
        public virtual Int16 Id_Tipo_Fattura { get; set; }          
        public virtual Int16 Cancellato { get; set; }
    }
}

删除事件监听器

    public class MyDeleteEventListener : DefaultDeleteEventListener
    {
        protected override void DeleteEntity(IEventSource session, object entity,
            EntityEntry entityEntry, bool isCascadeDeleteEnabled,
            IEntityPersister persister, ISet transientEntities)
        {
            if (entity is ISoftDeletable)
            {
                var e = (ISoftDeletable)entity;
                e.Cancellato = 1;

                CascadeBeforeDelete(session, persister, entity, entityEntry, transientEntities);
                CascadeAfterDelete(session, persister, entity, transientEntities);

            }
            else
            {
                base.DeleteEntity(session, entity, entityEntry, isCascadeDeleteEnabled,
                                  persister, transientEntities);
            }
        }
    }

以这种方式配置

configuration.SetListener(ListenerType.Delete, new MyDeleteEventListener());

事件触发,但未设置Cancellato = 1。 这是我的DeleteCommand

   public void Delete(T entity)
    {
        NHibernateSession.Delete(entity);

        NHibernateSession.Flush();
    }

1 个答案:

答案 0 :(得分:0)

我的地图文件出错。物业

 <property name="Cancellato" column="Cancellato" />

在我的数据库列中没有相同的名称。这样它就不会更新了。我只是不明白为什么在运行时没有错误......

我设置了相同的名字并且工作正常。