以下代码适用于插入但更新modifier
从未设置,任何想法为什么?
正在运行预更新代码,并将状态和实体值正确设置为所需的值。但是,在查看生成的sql时,nhibernate不包括更新查询中的字段。
/// <summary> Updates auditable objects </summary>
public class AuditEventListener : IPreInsertEventListener, IPreUpdateEventListener
{
private ISecurityManager securityManager;
public bool OnPreInsert( PreInsertEvent args )
{
var auditable = args.Entity as IAuditable;
if (auditable != null) {
Set( x => auditable.Creator, args.Persister, auditable, args.State, SecurityManager.Identity );
Set( x => auditable.DateAdded, args.Persister, auditable, args.State, Clock.Now );
}
return false;
}
public bool OnPreUpdate( PreUpdateEvent args )
{
var auditable = args.Entity as IAuditable;
if (auditable != null) {
Set( x => auditable.Modifier, args.Persister, auditable, args.State, SecurityManager.Identity );
//Set( x => auditable.DateModified, args.Persister, auditable, args.State, Clock.Now );
}
return false;
}
/// <summary> Type safe method to update sate and entity </summary>
private void Set<T, U>( Expression<Func<U, T>> expression, IEntityPersister persister, U instance, object[] state, T value )
{
var member = expression.Body as MemberExpression;
if (member != null) {
var index = Array.IndexOf( persister.PropertyNames, member.Member.Name );
if (index == -1) {
return;
}
state[index] = value;
var property = (member.Member as PropertyInfo);
if (property != null) {
property.SetValue( instance, value, null );
}
}
}
ISecurityManager SecurityManager
{
get { /* From IoC */ }
}
}
答案 0 :(得分:3)
编辑1:此答案已得到改善 编辑2:问题的真正原因是动态更新设置为真为found here但是此解决方案仍适用于我。
在先前调用的OnFlushDirty函数中更新它们时会保存更改。
public override bool OnFlushDirty( object entity, object id, object[] currentState, object[] previousState, string[] propertyNames, NHibernate.Type.IType[] types )
{
bool result = false;
if (entity is IAuditable) {
var auditable = (IAuditable)entity;
Set( x => auditable.Modifier, propertyNames, auditable, currentState, SecurityManager.Identity );
//Set( x => auditable.DateModified, args.Persister, auditable, args.State, TwentyClock.Now );
result = true;
}
return result;
}