What is the proper way to handle non-tracking self tracking entities?
- by Will
Self tracking entities. Awesome.
Except when you do something like
return Db.Users;
none of the self-tracking entities are tracking (until, possibly, they are deserialized).
Fine. So we have to recognize that there is a possibility that an entity returning to us does not have tracking enabled.
Now what???
Things I have tried
For the given method body:
using (var db = new Database())
{
if (update.ChangeTracker.ChangeTrackingEnabled)
db.Configurations.ApplyChanges(update);
else
FigureItOut(update, db);
db.SaveChanges();
update.AcceptChanges();
}
The following implementations of FigureItOut all fail:
db.Configurations.Attach(update);
db.DetectChanges();
Nor
db.Configurations.Attach(update);
db.Configurations.ApplyCurrentValues(update);
Nor
db.Configurations.Attach(update);
db.Configurations.ApplyOriginalValues(update);
Nor
db.Configurations.Attach(update);
db.Configurations.ApplyChanges(update
Nor about anything else I can figure to throw at it, other than
Getting the original entity from the database
Comparing each property by hand
Updating properties as needed
What, exactly, am I supposed to do with self-tracking entities that aren't tracking themselves??