What happens if I attach an object that is already attached in EF?
I store some EF entities
in a cache layer that I wrote. When I fetch them from the cache, I sometimes get the "contextObject isposed" error. I want to add a new one contextObject
after getting the entity from the cache. What side effects can this cause? What happens if I attach an object that is already attached? Performance?
source to share
try to do this.
ObjectStateEntry entry;
if(context.ObjectStateManager.TryGetObjectStateEntry(entity, out entry)) {
return (entry.State != EntityState.Detached);
}
check this answer also. Object bound to data context
source to share
It will throw an exception (not sure what) because the object is already attached.
Just try in code, it's very easy to try. It happened to me.
By the way, you should get rid of your context object as soon as you finish using it. You will get some weird behavior if not (objects don't update, cached objects changed directly in db don't reflect changes, etc.).
Sincerely.
source to share