2
votes

All my entities have the following properties:

  • public virtual DateTime CreatedDate {get; set;}
  • public virtual string CreatedBy { get; set; }
  • public virtual DateTime UpdatedDate { get; set; }
  • public virtual string UpdatedBy { get; set; }

I've implemented a EventListener (IPreUpdateEventListener, IPreInsertEventListener) so that I can fill these properties before inserting/updating my entity.
I do not load my entity just before saving cause I've got all my fields in a view (id, version). The only thing I would like to avoid to put in hidden fields (of my view) are CreatedDate and CreateBy.
Since I fill these fields only once, when the entity is created the first time, I was wondering if there's a way to exclude them when I update (only update) my entitiy?! Thanks.

1
unless you either change those fields with new data or force update on all fields the framework is smart enough not to include the unchanged fields in the update query, try to profile the calls and see if that is the case - Kris Ivanov
@K Ivanov -- That's not correct, by default NHibernate includes all mapped properties in an update even if they haven't changed. You can control this behavior using dynamic-update. - Jamie Ide

1 Answers

4
votes

Yes there is. For Fluent NHibernate mapping use

Map(x => x.CreatedDate).Not.Update();
Map(x => x.CreateBy).Not.Update();

for XML use

<property name="CreatedDate" update="false" />
<property name="CreateBy" update="false" />