I happened to work on a "visitor" for the graph of objects of the object. When I saw your question, I gave him the final touch to make it useful in your case (and many others). This is not a real visitor, as in the well-known visitor template, but he does basically the same thing: he crosses the graph of objects and performs some action for every entity he encounters.
Using this method, you can simply call ...
cc.Visit(product, e => cc.Entry(e).Reload());
... and you will see that product and all adhesive objects are reloaded.
Here is the code:
public static class DbContextExtensions { public static void Visit(this DbContext context, object entity, Action<object> action) { Action<object, DbContext, HashSet<object>, Action<object>> visitFunction = null;
This is a recursive function wrapped in an extension method. I wrapped the recursive part so that I can send a local HashSet on a schedule that collects visited objects and thereby prevents circular references. Basically, a function applies the specified action to an entity, and then finds that its navigation properties, which can be links or collections, get their values ββ( CurrentValue ), and then calls themselves for these values.
Please note that I also check if the navigation properties are loaded. Without this, an endless chain of lazy loading can begin.
Also note that this raises one query for each object in the graph. This is not suitable for large object graphs. If you want to update large amounts of data, you should use a different approach, it is preferable to create a new context.
Gert arnold
source share