Getting Entity Framework’s ExecuteStoreQuery Method to Return Attached Objects

When you call myObjectContext.ExecuteStoreQuery<TEntity>(string commandText, param object[] parameters), the objects returned are detached from the object context. You might have to jump through some hoops to get them attached to your context. (It wants to treat them as new instances, and you’re like WTF.)

If you want the returned entities to be added to your context, there’s an overload to ExecuteStoreQuery that lets you specify an entitySetName parameter and a mergeOption parameter. The entitySetName parameter is the name of the entity set in the object context you want your objects to go into, and the mergeOption specifies options about whether the results returned should overwrite stuff that might already be in your object context.