使用事件侦听器时 NHibernate 不更新父对象的表
我试图在使用 NHibernate 事件侦听器插入或更新域对象之前将审核字段添加到域对象中。利用 IPreInsert
和 IPreUpdate
事件侦听器接口,我可以在所有域对象上设置审核字段,但那些审核字段存储在父对象表中的对象除外。例如,以下是我的实体类:
public class Person : Entity
{
public string name {get; set;}
}
public class Entity : PersistentEntity
{
public int EntityTypeId {get; set;}
}
public abstract class PersistentEntity
{
public virtual Guid Id { get; set;}
public virtual Guid UpdateUser {get; set;}
public virtual DateTime UpdateDate {get; set;}
}
这是我的映射:
public class PersonMap : SubclassMap<Person>
{
Map(x => x.Name);
}
public class EntityMap : PersistentEntityMap<Entity>
{
Map(x => x.EntityTypeId);
}
public abstract class PersistentEntityMap<T> : ClassMap<T> where T : PersistentEntityMap
{
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.UpdateUser);
Map(x => x.UpdateDate);
}
以下是表的定义方式:
Person Table:
- Id
- Name
Entity Table:
- Id
- EntityTypeId
- UpdateUser
- UpdateDate
因此,根据此设置,IPreInsert
事件侦听器工作完美,将一个新人员插入人员表中,并将一个新的 Entity
插入 Entity
表中,并使用 UpdateUser
和 UpdateDate< /代码>正确填充。但是,如果我要更新人员姓名,
IPreUpdate
事件侦听器会触发并更改对象上的 UpdateDate
字段,但永远不会执行实体表上的更新。从此页面获取建议,
http://nhforge.org/wikis /howtonh/changing-values-in-nhibernate-events.aspx
我尝试使用 IFlushEntity
和 ISaveOrUpdate
事件侦听器,但没有 影响。
I'm trying to add audit fields to my domain objects before they are inserted or updated by utilizing NHibernate event listeners. Utilizing the IPreInsert
and IPreUpdate
event listener interfaces, I am able to set the auditing fields on all the domain objects except for those that have the auditing fields stored in a parent object's table. For example, here are my entity classes:
public class Person : Entity
{
public string name {get; set;}
}
public class Entity : PersistentEntity
{
public int EntityTypeId {get; set;}
}
public abstract class PersistentEntity
{
public virtual Guid Id { get; set;}
public virtual Guid UpdateUser {get; set;}
public virtual DateTime UpdateDate {get; set;}
}
Here are my mappings:
public class PersonMap : SubclassMap<Person>
{
Map(x => x.Name);
}
public class EntityMap : PersistentEntityMap<Entity>
{
Map(x => x.EntityTypeId);
}
public abstract class PersistentEntityMap<T> : ClassMap<T> where T : PersistentEntityMap
{
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.UpdateUser);
Map(x => x.UpdateDate);
}
And here is how the tables are defined:
Person Table:
- Id
- Name
Entity Table:
- Id
- EntityTypeId
- UpdateUser
- UpdateDate
So given this setup, the IPreInsert
event listener works perfectly, inserting a new person into the person table and inserting a new Entity
into the Entity
table with UpdateUser
and UpdateDate
correctly populated. However, if I where to update the person's name, the IPreUpdate
event listener fires and does change the UpdateDate
field on the object, but an update on the entity table is never performed. Taking advise from this page,
http://nhforge.org/wikis/howtonh/changing-values-in-nhibernate-events.aspx
I tried using both IFlushEntity
and ISaveOrUpdate
event listeners to no effect.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论