0

I am developing an mvc application and I am using fluent nhibernate and enverse to store information in db and to auditing my data.

My base class for entities looks like:

[Audited]
public class EntityBase : IEntity
{
    public virtual int ID { get; set; }
    public virtual string CreatedBy { get; set; }
    public virtual string ModifiedBy { get; set; }
    public virtual DateTime CreatedDate { get; set; }
    public virtual DateTime? ModifiedDate { get; set; }
    public virtual bool IsActive { get; set; }
}

My fluent mapping:

internal class EntityMap<T> : ClassMap<T> where T:EntityBase
{
    public EntityMap(string tableName = null)
    {
        Id(x => x.ID).DefaultStrategy<T>(tableName);

        if (!string.IsNullOrEmpty(tableName))
            base.Table(tableName);

        Map(x => x.IsActive).Not.Nullable();
        Map(x => x.CreatedDate).Not.Nullable().Not.Update().CustomType("Timestamp");
        Map(x => x.CreatedBy).Not.Update();
        Map(x => x.ModifiedDate).CustomType("Timestamp");
        Map(x => x.ModifiedBy);
    }
}

When I store each entity to db then action on pre insert is running and it sets values for createdDate and createdBy.

I have a problem when i am updating an entity. CreatedDate and CreatedBY are not update fields so I don't store this information in my viewmodel so during conversion to model/entity a have no information about createdDate and createdBy. In my table I have all information after edit and entitybase but in auditing I receive a new row with empty CratedDate and CreatedBy.

is there any way to configure enverse to not lose this data?

Marek
  • 193
  • 1
  • 2
  • 11
  • Yes, that should work I think. IIRC the preinsert event handler have both state and entity instance. Are you updating them both? Also, the order of you listener and envers listener might matter. Are tou registring yours before envers? – Roger Jan 16 '15 at 18:13
  • On the other hand, I don't understand why you need those props if youre using envers? Dont you have user and time on your revision entity? – Roger Jan 16 '15 at 18:14
  • Answering second question: it was created before including enverse in our app. Moreover it helps when you want to display some data in a grid. Because it is easy to implement sorting on db. First question. It doesn't work. "Also, the order of you listener and envers listener might matter. Are tou registring yours before envers?" I dont use envers listeners on demand I use only attributes above entities and that's all. – Marek Jan 20 '15 at 08:08
  • Envers is appending its event listeners when you call IntegrateWithEnvers on the Configuration object. – Roger Jan 22 '15 at 10:28

0 Answers0