DbEntityEntry.OriginalValues not populating complex properties

I'm writing an audit trail from snippets of code found online. On the call to my SaveChanges function I loop through all the modified entities registered with the Context and build log entries from their changes.

foreach (DbEntityEntry modifiedEntity in this.ChangeTracker.Entries().Where(p => p.State == System.Data.EntityState.Added || p.State == System.Data.EntityState.Deleted || p.State == System.Data.EntityState.Modified))
        {
            // For each changed record, get the audit record entries and add them
            foreach(AuditLog x in GetAuditRecordsForChange(modifiedEntity, userId))
            {
                this.AuditLog.Add(x);
            }
        }

When I then try to access the original values of the modified entity, all the scalar properties are populated but the complex ones don't exist (property count will be say 6 instead of 8). I then call ToObject() to build the object in its original state but obviously the complex properties are all nulls.

modifiedEntity.OriginalValues.ToObject()

This only happens with some of my domain objects, and those objects always show as proxies after the ToObject() call whereas (I'm not sure why) but the ones that don't have proxies created for them by entity, their complex properties populate fine. When I'm using the POCO proxies as normal throughout my application, lazy loading works fine on them.

I've noticed that if I make a change to one of these complex properties that are not populated as part of the OriginalValues data, the object's state doesn't get changed to Modified, this makes sense as change tracking compares the original values to current to see if it's changed. What doesn't make sense is that the data is still persisted on SaveChanged??

EDIT: I've just noticed, the model object that does populate its complex properties, the complex property in question is (by convention) considered a 'complex type' by Entity i.e no primary key.

Any ideas?

5
задан SeeNoWeevil 11 August 2011 в 12:30
поделиться