What is the proper way to handle non-tracking self tracking entities?

Posted by Will on Stack Overflow See other posts from Stack Overflow or by Will
Published on 2010-03-22T20:17:59Z Indexed on 2010/03/22 20:21 UTC
Read the original article Hit count: 483

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

  1. Getting the original entity from the database
  2. Comparing each property by hand
  3. Updating properties as needed

What, exactly, am I supposed to do with self-tracking entities that aren't tracking themselves??

© Stack Overflow or respective owner

Related posts about ef4

Related posts about self-tracking-entities