I am writing an audit trail of code fragments found on the Internet. When I call my SaveChanges function, I look through all the changed objects registered in the Context and recording the 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)) {
When I then try to access the initial values of the modified object, all scalar properties are populated, but there are no complex ones (the number of properties will be considered 6 instead of 8). Then I call ToObject() to build the object in its original state, but obviously all zeros are complex properties.
modifiedEntity.OriginalValues.ToObject()
This only happens with some of my domain objects, and these objects always appear as proxies after calling ToObject() , whereas (I'm not sure why), but those that do not have proxies created for them by the entity, their complex properties are well filled. When I use POCO proxies as usual throughout the application, lazy loading works fine for them.
I noticed that if I make changes to one of these complex properties that are not populated as part of the OriginalValues data, the state of the object does not change to Modified, this makes sense, because change tracking compares the original value for the current to see if it has changed. What does not make sense is that the data is saved on SaveChanged ??
EDIT: I just noticed that the model object that fills its complex properties is a complex property (by convention), considered as a "complex type" by Entity i. There is no primary key.
Any ideas?
SeeNoWeevil Aug 11 '11 at 8:10 2011-08-11 08:10
source share