3

我想对我的数据库表进行软删除...我已经应用了以下语句(如此处所述http://nhibernate.info/blog/2008/09/06/soft-deletes.html和很多问题关于 SO)。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());

事件触发但它没有设置 Cancelato =1。这是我的删除命令

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

        NHibernateSession.Flush();
    }
4

1 回答 1

0

我的映射文件有错误。财产

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

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

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

于 2012-09-10T07:54:48.417 回答