auto update fields on savechanges

2 posts, 0 answers
  1. CreoniX
    CreoniX avatar
    27 posts
    Member since:
    Sep 2009

    Posted 23 Feb 2015 Link to this post

    In the past we updated 2 properties on savechanges (lastUpdate (datetime) and LastStaffID (Guid)) but i can't seem to find out how i can manage this with DataAccess, only found a 1 field option so far.

    Any suggestion on how i can achieve this?

    example of how it was : 

    foreach (Entity item in e.Entities)
                    if (item.EntityAspect.EntityState == EntityState.Modified)
                            item.EntityAspect.EntityMetadata.DataProperties["LastUpdate"].SetValue(item, DateTime.Now);
                            item.EntityAspect.EntityMetadata.DataProperties["LastStaffID"].SetValue(item, Guid.Empty);
                        catch (Exception)
                        { ; }
  2. Kaloyan Nikolov
    Kaloyan Nikolov avatar
    118 posts

    Posted 26 Feb 2015 Link to this post


    This can be easily achieved if you redefine the SaveChanges implementation in a partial class of you context. The code should something like this:

    public partial class EntitiesModel1
        public new void SaveChanges()
            var changes = base.GetChanges();
            var updates = changes.GetUpdates<IEnitity>();
            foreach (var u in updates)
                u.ModifiedBy = "theUser";
                u.ModifiedOn = DateTime.Now;

    Here I rely on the fact that all entities in the model inherit from the IEntity interface where the ModifiedOn property is defined. You can define the interface in the Designer or as code if you are using fluent mapping. The code above will update the ModifiedOn property for each updated record that inherits from the IEntity interface. 

    I hope this helps. Should you have any additional questions do not hesitate to get back to us. 

    Kaloyan Nikolov
    OpenAccess ORM is now Telerik Data Access. For more information on the new names, please, check out the Telerik Product Map.
  3. DevCraft banner
Back to Top