我希望做一个在我的数据库表软删除...我有申请下面的语句(如这里所描述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());
事件火,但它不设置Cancellato = 1。 这是我的DeleteCommand
public void Delete(T entity)
{
NHibernateSession.Delete(entity);
NHibernateSession.Flush();
}