软删除Nhibernate [英] Soft Delete Nhibernate
问题描述
我想对我的数据库表进行软删除...我已经应用了以下语句(如此处所述
I want to make a soft delete on my db table...i have apply following statement (as described here http://nhibernate.info/blog/2008/09/06/soft-deletes.html and in a lot of question on SO). Fattura is my table where i want apply logical delete (there is no trigger on it)
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
The event fire but it doesn't set Cancellato =1. This is my DeleteCommand
public void Delete(T entity)
{
NHibernateSession.Delete(entity);
NHibernateSession.Flush();
}
推荐答案
我的映射文件存在错误.财产
There is an error on my mapping file. Property
<property name="Cancellato" column="Cancellato" />
我的数据库列中没有相同的名称.这样就不会更新.我只是不明白为什么在运行时没有错误...
haven't same name in my database column. In that way it's not updated. I just don't understand why at runtime there is no error...
我设置了相同的名称,并且工作正常.
I have set same name and work fine.
这篇关于软删除Nhibernate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!