Entity Framework Structural Change Tracking API and Reference

Want to write a generic audit code in my subclass of DbContext.

foreach (var entry in this.ChangeTracker.Entries<MyClass>()) { if (entry.State == EntityState.Modified) { var entityProperties = entry.Entity.GetType().GetProperties(); foreach (var entityProperty in entityProperties) { DbMemberEntry propertyEntry = entry.Member(property.Name); if (propertyEntry is DbPropertyEntry) { // IsModified available } else if (propertyEntry is DbReferenceEntry) { // IsModified not available } } } } 

1) If I change only the link property, the value of entry.State is "No change."

2) Even if item 1 is set to Modified, the DbReferenceEntry class does not seem to have the IsModified property and the original value.

I guess this is possible because EF should keep track of this.

Can anyone help?

Thanks Ben

+7
source share
1 answer

Yes, this link (navigation property) does not track changes. This is the responsibility of a foreign key property (in the case of a foreign key association) or individual changes to tracking objects of an independent association. In the ObjectContext API, you can get these objects using the ObjectStateManager , but it looks like the DbContext API does not. I asked a question about this on the MSDN forum.

+6
source

All Articles