问题
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 - Model Object
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; }
}
}
Delete event Listener
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);
}
}
}
Configured in that way
configuration.SetListener(ListenerType.Delete, new MyDeleteEventListener());
The event fire but it doesn't set Cancellato =1. This is my DeleteCommand
public void Delete(T entity)
{
NHibernateSession.Delete(entity);
NHibernateSession.Flush();
}
回答1:
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.
来源:https://stackoverflow.com/questions/12296076/soft-delete-nhibernate