Exception in Expression Trees

entity-framework graphdiff


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.Business 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 internalInclude call (to include object graph to load store object) tries to connectWorkingPeriods 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 =>

The stacktrace for the error is Here.

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.

5/23/2017 12:06:17 PM

Accepted Answer

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.

7/13/2013 5:26:12 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow