I'm using EntityFramework 5 running on .NET 4.5
I'm trying to perform a record update, using approach as below:
var rec = DbCntx.MyRecords.Attach(changedRecord);
var entry = DbCntx.Entry(changedRecord);
if (entry.State == EntityState.Modified) DbCntx.SaveChanges();
The passed in data object changedRecord contained with at least 1 field value modified, compare to the actual db. However the Entry.State is always Unchanged, so SaveChanges() never triggered.
I'm writing this update routine to support any data changes over the changedRecord, the data changes is totally depend on the client/caller, so I shouldn't specifically change any field value in this routine.
My problem using this approach, I can't get the original data values. All the entity I can accessed are the exact of changedRecord, which is as a disconnected record with changes I haven't commit into the db.
Without the original data, I can't identify which properties are changes. If I try to use Find(), First/SingleOrDefault() to locate the record 1st, I can't use Attach() later as it would cause error "ObjectStateManager cannot track multiple objects with the same key".
from Googling, some hinted to use Detach(), but Detach is not available in DbContext.