This is how I'd act:
- Business - BusinesType - FK - Categories (*) - FK - Branch (*) - BranchType - FK - Address - Phone (*) - CustomFields (*) - OpeningTimes (*) - WorkingPeriods (*) - .....
I now have a controller action that accepts a form with all of the data in one batch.
entity that has all of its collections and properties set correctly.
Now I have to iteratively go through all the collections and properties and compare them to the database graph; if they don't exist, add them; if they do, go through all the properties once more and carry out the same action to a deeper level until there are no navigation properties left over. It's easy to manually browse them as I have many more characteristics and descendants than were specified in the previous example.
I discovered GraphDiff, which provided a fantastic solution, thanks to this answer.
I'm calling for an update at this time:
Context.UpdateGraph(business, bus => bus .AssociatedEntity(bu => bu.BusinessType) .AssociatedCollection(bu => bu.Categories) .OwnedCollection(bu => bu.Branches, branch => branch .AssociatedEntity(b => b.BranchType) .OwnedEntity(b => b.Address) .OwnedCollection(b => b.Phones) .OwnedCollection(b => b.CustomFields) .OwnedCollection(b => b.OpeningTimes, openingTimes => openingTimes .OwnedCollection(b => b.WorkingPeriods) ) ) );
This exception is raised by it:
System.InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.MethodCallExpressionN' to type 'System.Linq.Expressions.MemberExpression'.
I attempted to troubleshoot the source code, but I'm not very knowledgeable with Expression Trees. The issue arises when the internal
call (to include object graph to load store object) tries to connect
appears to not be prepared to handle that amount of recursion depth. I played around with it a little, but I'm confident that someone who is well-versed in expression trees would be able to resolve this without any difficulty. On that subject, I'd also appreciate any advice.
The include path expression should be created as shown below:
.Include(b => b.Branches.Select(br => br.OpeningTimes.Select(ot => ot.WorkingPeriods)));
Essentially, the recursive call returns the inner include as a method call rather than processing it and returning the collection property it's intended to expose, which is why the exception is raised.
I apologize for the delay in responding to you.
Even though it's 3 am and I've had a lot of alcohol, the issue has been resolved. The most recent code may be found in https://github.com/refactorthis/GraphDiff, and should function without a problem.
I'll shortly update the brand-new RefactorThis.GraphDiff nuget package.