0

I used the code from the Override Save Changes question to implement auditing in my application. When an entity is added everything is great. However, when an entity is modified the "CreatedOn" date is never loaded.

It is always null, so the initial "CreatedOn" date gets removed from the database.

I also tried looking in the OriginalValues collection stored in the entity, and it is populated for the "ModifiedOn" date but not for the "CreatedOn" date. Both fields are populated in the database...why would one be loaded by EF and one not be loaded?

public override int SaveChanges()
    {
        var changeSet = ChangeTracker.Entries<IAuditable>();

        if (changeSet != null)
        {
            foreach (DbEntityEntry<IAuditable> entry in changeSet)
            {
                switch (entry.State)
                {
                    case EntityState.Added:
                        entry.Entity.CreatedOn = DateTime.Now;
                        entry.Entity.ModifiedOn = DateTime.Now;
                        break;
                    case EntityState.Modified:
                        entry.Entity.ModifiedOn = DateTime.Now;

                        //entry.Entity.CreatedOn date always null here
                        break;
                }
            }
        }

         return base.SaveChanges();
    }
Community
  • 1
  • 1
Neil
  • 737
  • 1
  • 8
  • 23
  • Did you find a solution for this? Today I hit almost exactly the same issue as you, only in my case it is `UpdatedBy`/`UpdatedById` that comes null. Interestingly, `UpdatedOn` works just fine, even though when I check the row in SSMS, neither `UpdatedOn` or `UpdatedById` are NULL. I'm wondering if the audit fields are somehow clashing with one another, providing that all three of them point to the same entity... – Daniel Liuzzi May 02 '12 at 02:09
  • 1
    I never did find a solution for this - I ultimately ended up reloading the created on date from the database before saving. Not ideal to have the extra database call, but it works. – Neil May 02 '12 at 14:17
  • 1
    Last night I kept digging on this, and found out it is DBSet.Remove(entity) what set my properties to null (this is before even calling `SaveChanges()`). This apparently is [by design](http://stackoverflow.com/a/6782744/88709), which is a shame, because I ended up having to move the logic up a layer into my repositories. I would much rather have the soft delete implemented in DbContext, as it is more DRY. Thanks for your update anyway! – Daniel Liuzzi May 02 '12 at 14:28

1 Answers1

2

Maybe you are using in a MVC context? If it is, it's maybe just because you don't put your entity properties CreatedOn and ModifiedOn in an hidden field of your page. I got this problem and I solve it with a partial view.

/*My Partial View View/Shared/HiddenSystemValues.cshtml */
@model Models.IAuditable
@Html.HiddenFor(model => model.CreatedOn)
@Html.HiddenFor(model => model.ModifiedOn)
...


/*In All My Pages */
...
@Html.Partial("HiddenSystemValues")
...
animuson
  • 53,861
  • 28
  • 137
  • 147
IV V
  • 71
  • 3