I’ve a procedure where I need to save an entity object. The problem is that I don’t know if this entity is attached to my datacontext or not. To solve this I use the following code:
try { db.ClientUsers.Attach(clientUser); db.Refresh(RefreshMode.KeepCurrentValues, clientUser); } catch { } db.SubmitChanges();
I’m looking for a better method to detect if an entity belongs to a context and also to test if an entity is attached to a specific context.
I wonder… what does
GetOriginalEntityStatereturn for a non-attached object? Even if it throws an exception, it’ll probably be faster than a refresh…(update) – a test shows it returns null:
So perhaps use
GetOriginalEntityStateand check for null returned value…