1

Ok there's a lot going on here and I don't want to bore you guys with a very long winded code sample so here's an extract ...

When SaveChangesAsync() is called on my EF context I have it calling this method to audit each entry ...

async Task Audit(DbEntityEntry<IAmAuditable> entry)
{
    try
    {
        var newAuditEntry = new AuditEntry
        {
            EntityType = entry.Entity.GetType().Name,
            Event = entry.State.ToString(),
            SSOUserId = kernel.Get<User>().Id,
            EntityId = entry.GetId().ToString(),
            EventId = eventId
        };

In the event that the entry in question is an entity creation which will result in an insert on the db i then also do this ...

var properties = entry.CurrentValues.PropertyNames.Select(p => entry.Property(p)).ToList();
var addedValues = new List<AuditDataItem>();

foreach (var p in properties)
{
    addedValues.Add(new AuditDataItem
    {
        PropertyName = p.Name,
        PreviousValue = null,
        NewValue = p.CurrentValue.ToString()
    });
}
newAuditEntry.Changes = addedValues;
break;

... this is where it falls over ... at that point in time the base call to SaveChanges hasn't yet been executed so the entity in question does not yet have a primary key value ... the net result is that I log the creation of an entity with no primary.

Does anyone have suggestions on a nice clean way to handle this so I can put the new primary key value in to an AuditDataItem?

EDIT:

Here's an example of what I am logging at the moment as json, this is a single AuditEntry object and a partial of some of the child AuditDataItem rows ...

   {
      "Id": 4,
      "SSOUserId": 1,
      "EventId": "6d862aad-0898-4794-aea0-00af6f2994ff",
      "EntityType": "AC_Programme",
      "Event": "Added",
      "TimeOfEvent": "2016-02-04T12:04:31.5501508+01:00",
      "Changes": [
        {
          "Id": 34,
          "PropertyName": "Id",
          "PreviousValue": null,
          "NewValue": "0"
        },
        {
          "Id": 35,
          "PropertyName": "Name",
          "PreviousValue": null,
          "NewValue": "Test"
        },
        ...
      ]
    }

3 Answers 3

1

As far as I know there are no events to intercetp object creation, after the object has been created. There are events for:

The first happens before the changes are saved (same problem you have). The second one happens when an entity is read from the database as result of a query or .Load, so it doesn't fit your case.

The only solution I can think of is that you override the original SaveChanges, and do this in the overridden method:

  • look for all the entities in the Added state, and keep references to them, for example adding them in a List<Object>
  • call the base save changes, so that the changes are materialized in the database, and the entities in the DbCOntext get updated
  • access the entities in the list, and you'll get all the db generated properties (like calculated, identities, guid and so on), so that you can log them correctly

The Logging and Intercepting Database Operations (EF6 Onwards) doesn't fit your needs

4
  • Hi JotaBe, that's exactly what I am doing in my overridden SaveChangesAsync() method im calling AuditChanges() then calling the base Savechanges method, Audit changes generates these extra insertions in to my custom AuditEntry table and its child AuditDataItems. My gut feeling is that I can only audit creations after the base Savechanges has been executed but i'm guessing that if I query the change tracker at that point it'll tell me nothing is changed, so i can either get the not yet created entity or get nothing ...
    – War
    Feb 5, 2016 at 11:31
  • Its worth noting here that for logging / debugging reasons I am also intercepting the SQL but this is for auditing so i'm not interested in the sql only the row values that result in the before and after for the sql execution ... am i making sense?
    – War
    Feb 5, 2016 at 11:33
  • @Wardy in answer to your first question: 1st step: keep list of added entities 3rd step: use that list. Note that 1st step is before SaveChanges, so you'll find the modified entities. And in the 3rd step you can access them from the list stored in the 1st step, and you'll find the modified (now unchanged) version of teh entities. EF modifes the entities in teh dbcontext, don't dispose of them and create new ones. That's why this works.
    – JotaBe
    Feb 5, 2016 at 13:08
  • this combined with Jonathons library gives me some ideas, I'm going to +1 try something out ... you're right though, there are essentially 3 steps in the process and entities that are being added can only be audited after the add has taken place, i probably just need to refactor my calling code to call my audit method for non adds before the save takes place and adds after the save takes place
    – War
    Feb 5, 2016 at 14:34
1

You need to keep the ObjectStateEntry in your AuditEntry then revisit every AuditEntry which the primary key was "Temporary" in a PostSaveChanges event.

Here is an example:

Obviously I recommend you to use EF+ Audit over creating your own library but if you still want to code it, the library is open source so you will be able to find a lot of information to help you.

Disclaimer: I'm the owner of the project EF+ (EntityFramework Plus)

2
  • +1 Thanks for the complete solution, and using a library is one way to go but given that I have effectively the same solution in under 50 lines of code I think pulling in a whole new library is overkill. Looking at your solution I can implement the key parts with about an extra 5 to 10 lines for my case ... interesting approach!
    – War
    Feb 5, 2016 at 14:30
  • Yup, I agree at 100% with you. This will be overkill for your situation. Feb 5, 2016 at 14:41
0

Ok so here's what i came up with ... curious to know what you guys think ...

public override async Task<int> SaveChangesAsync()
{
    try
    {
        await AuditChanges(new[] { EntityState.Modified, EntityState.Deleted });
        var result = await base.SaveChangesAsync();
        await AuditChanges(new[] { EntityState.Added });
        return result;
    }
    catch (DbEntityValidationException ex) { throw ConstructDetailsFor(ex); }
}

async Task AuditChanges(EntityState[] states)
{
    var auditableEntities = ChangeTracker.Entries<IAmAuditable>()
        .Where(e => states.Contains(e.State));

    foreach (var entry in auditableEntities)
        await Audit(entry);
}

async Task Audit(DbEntityEntry<IAmAuditable> entry)
{
    ...

This is about as simple as it gets :)

My audit method then basically enters in to a switch statement and decides what logic to run based on the passed in auditable entity entry from the change tracker.

I don't think auditing could get much simpler than this.

I put this in to a generic base class for all my EF contexts and run a migration to apply this to all db's and bang ... everywhere gets dynamic, auto auditing on all entities that are marked with "IAmAuditable" (an empty marker interface).

I thought about using an attribute but that would require reflection and what not.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.