表达式树中的异常 [英] Exception in Expression Trees

查看:74
本文介绍了表达式树中的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的模型:

 - Business
   - BusinesType - FK
   - Categories (*) - FK
   - Branch (*)
     - BranchType - FK
     - Address
     - Phone (*)
     - CustomFields (*)
     - OpeningTimes (*)
       - WorkingPeriods (*)
   - .....

现在,我有一个控制器动作,该动作将包含全部数据的表单作为单个 Business 实体接受,其所有属性和集合都设置正确。

Now I have a controller-action that accepts a form that consists of the whole bunch of data as a single Business entity with all its properties and collections set fine.

现在,我必须递归遍历所有属性和集合,并与数据库图形进行比较;如果它们不存在,则添加它们;如果它们确实再次遍历所有属性,并执行相同的操作,直到没有剩余导航属性为止。由于我的属性和后代比上一个示例中提及的要多得多,因此可以通过它们手动进行操作。

Now I have to walk thru all the properties and collections recursively, and compare with the database graph; if they don't exist, add them, if they do walk thru all properties again and perform the same to a deeper level until no navigation properties are left over. Since I have many more properties and descendants than mentioned in the previous example, it's just inside to walk thru them manually.

感谢这个答案是GraphDiff,它为这种情况提供了一个绝妙的解决方案。

Thanks to this answer I found GraphDiff which offered a brilliant solution to the situation.

这里是我的更新查询m调用:

Here's an update query I'm calling:

    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)
        )
      )
    );

抛出此异常:


System.InvalidCastException:无法将类型为'System.Linq.Expressions.MethodCallExpressionN'的对象转换为类型为'System.Linq.Expressions.MemberExpression'。

System.InvalidCastException: Unable to cast object of type 'System.Linq.Expressions.MethodCallExpressionN' to type 'System.Linq.Expressions.MemberExpression'.

我尝试调试源代码,但我不是Expression Trees的专家,当内部 Include 调用( (包括将对象图加载到存储对象的对象)尝试附加 WorkingPeriods ,似乎还没有准备好进行这种深度的递归。我有点弄乱了,但是我敢肯定,对表达式树有广泛了解的人将能够轻松解决此问题。

I tried debugging the source code, but I'm not an expert with Expression Trees, the problem occurs when the internal Include call (to include object graph to load store object) tries to attach WorkingPeriods, looks like it's not ready to take that deepness level of recursion. I messed around with it a bit, but I'm sure someone with extensive knowledge in expression trees will be able to solve this easily. Any suggestions will be appreciated on that to.

以下是应该生成包含路径表达式的内容:

Here's what the include path expression is supposed to be generated like:

.Include(b =>
  b.Branches.Select(br =>
    br.OpeningTimes.Select(ot =>
      ot.WorkingPeriods)));

这里错误的堆栈跟踪。

本质上,抛出异常是因为递归调用将内部include作为方法调用返回,而不进行处理并返回要公开的collection属性。

Essentially, the exception is thrown because the recursive call returns the inner include as a method call, without processing it and returning the collection property it's meant to expose.

推荐答案

抱歉,我花了一些时间才能回复您。

sorry it took me a while to get back to you.

我凌晨3点和我已经喝了很多酒,但是问题已经解决了:)如果您获得最新版本的代码@ https: //github.com/refactorthis/GraphDiff 应该可以正常工作。

I'ts 3am and I've had a fair bit of wine but the problem is fixed :) If you get the latest version of code @ https://github.com/refactorthis/GraphDiff it should work fine.

我将尽快更新新的nuget包(RefactorThis.GraphDiff)。

I'll update the new nuget package (RefactorThis.GraphDiff) soon.

这篇关于表达式树中的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆