Dealing with complex properties with Entity Framework's ChangeTracker

650 views Asked by At

I'll try and be as descriptive as I can in this post. I've read a dozen or more SO questions that were peripherally related to my issue, but so far none have matched up with what's going on.

So, for performing audit-logging on our database transactions (create, update, delete), our design uses an IAuditable interface, like so:

public interface IAuditable
{
    Guid AuditTargetId { get; set; }

    int? ContextId1 { get; }
    int? ContextId2 { get; }
    int? ContextId3 { get; }
}

The three contextual IDs are related to how the domain model is laid out, and as noted, some or all of them may be null, depending on the entity being audited (they're used for filtering purposes for when admins only want to see the audit logs for a specific scope of the application). Any model that needs to be audited upon a CUD action just needs to implement this interface.

The way that the audit tables themselves are being populated is through an AuditableContext that sits between the base DbContext and our domain's context. It contains the audit table DbSets, and it overrides the SaveChanges method using the EF ChangeTracker, like so:

foreach (var entry in ChangeTracker.Entries<IAuditable>())
{
    if (entry.State != EntityState.Modified && 
        entry.State != EntityState.Added && 
        entry.State != EntityState.Deleted)
    {
        continue;
    }

    // Otherwise, make audit records!
}

base.SaveChanges();

The "make audit records" process is a slightly-complex bit of code using reflection and other fun things to extract out fields that need to be audited (there are ways for auditable models to have some of their fields "opt out" of auditing) and all that.

So that logic is all well and good. The issues comes when I have an auditable model like this:

public class Foo: Model, IAuditable
{
    public int FooId { get; set; }

    // other fields, blah blah blah...

    public virtual Bar Bar { get; set; }

    #region IAuditable members

    // most of the auditable members are just pulling from the right fields

    public int? ContextId3
    {
        get { return Bar.BarId; }
    }

    #endregion
}

As is pointed out, for the most part, those contextual audit fields are just standard properties from the models. But there are some cases, like here, where the context id needs to be pulled from a virtual complex property.

This ends up resulting in a NullReferenceException when trying to get that property out from within the SaveChanges() method - it says that the virtual Bar property does not exist. I've read some about how ChangeTracker is built to allow lazy-loading of complex properties, but I can't find the syntax to get it right. The fields don't exist in the "original values" list, and the object state manager doesn't have those fields, I guess because they come from the interface and not the entities directly being audited.

So does anyone know how to get around this weird issue? Can I just force eager-loading of the entire object, virtual properties included, instead of the lazy loading that is apparently being stubborn?

Sorry for the long-ish post, I feel like this is a really specific problem and the detail is probably needed.

TIA! :)

0

There are 0 answers