问题 EF非静态方法需要目标


我对以下查询存在严重问题。

context.CharacteristicMeasures
        .FirstOrDefault(cm => cm.Charge == null &&
                              cm.Characteristic != null &&
                              cm.Characteristic.Id == c.Id &&
                              cm.Line != null &&
                              cm.Line.Id == newLine.Id &&
                              cm.ShiftIndex != null &&
                              cm.ShiftIndex.Id == actShiftIndex.Id &&
                              (newAreaItem == null ||
                                  (cm.AreaItem != null &&
                                   cm.AreaItem.Id == newAreaItem.Id)));

我得到了 TargetException: Non-static method requires a target  当newAreaItem为null时。 如果newAreaItem不为null,我得到一个 NotSupportedException: Unable to create a constant value of type 'PQS.Model.AreaItem'. Only primitive types or enumeration types are supported in this context.

我已经检查过的东西是否为空: c,newLine,actShiftIndex所有3个变量都不为空,并且可以访问Id。

我不明白......请帮忙。

如果你需要更多信息..不要犹豫,问...

UPDATE

我可以消除 NotSupportedException,但是当newAreaItemIsNull为true时,我仍然得到了TargetException ..:/

bool newAreaItemIsNull = (newAreaItem == null);

var mc = context.CharacteristicMeasures
                .FirstOrDefault(cm => cm.Charge == null &&
                                      cm.Characteristic != null &&
                                      cm.Characteristic.Id == c.Id &&
                                      cm.Line != null &&
                                      cm.Line.Id == newLine.Id &&
                                      cm.ShiftIndex != null &&
                                      cm.ShiftIndex.Id == actShiftIndex.Id &&
                                      (newAreaItemIsNull ||
                                          (cm.AreaItem != null &&
                                           cm.AreaItem.Id == newAreaItem.Id)));

UPDATE

我终于做到了。看来查询解析无法解析我的问题 newAreaItem(IsNull) 因为它不是以某种方式在数据库模型中! 我必须分开我的查询..

bool newAreaItemIsNull = (newAreaItem == null);

MeasureCharacteristic mc;

if (newAreaItemIsNull)
   mc = context.CharacteristicMeasures
               .FirstOrDefault(cm => cm.Charge == null &&
                                     cm.Characteristic != null &&
                                     cm.Characteristic.Id == c.Id &&
                                     cm.Line != null &&
                                     cm.Line.Id == newLine.Id &&
                                     cm.ShiftIndex != null &&
                                     cm.ShiftIndex.Id == actShiftIndex.Id);
else
   mc = context.CharacteristicMeasures
               .FirstOrDefault(cm => cm.Charge == null &&
                                     cm.Characteristic != null &&
                                     cm.Characteristic.Id == c.Id &&
                                     cm.Line != null &&
                                     cm.Line.Id == newLine.Id &&
                                     cm.ShiftIndex != null &&
                                     cm.ShiftIndex.Id == actShiftIndex.Id &&
                                     cm.AreaItem != null &&
                                     cm.AreaItem.Id == newAreaItem.Id);

有人知道更好的解决方案吗?


5455
2018-03-19 12:02


起源

可能重复 非静态方法需要目标。实体框架5代码优先 - Chris Moschini


答案:


试着移动 newAreaItem == null 在查询之外

bool newAreaItemIsNull = (newAreaItem == null);

并替换 newAreaItem == null 同 newAreaItemIsNull 在查询中。

查询解析器只能与数据库中的对象一起操作,而newAreaItem不是其中之一。


16
2018-03-19 12:14



谢谢。这有助于消除NotSupportedException。仍然当newAreaitem为null时,我得到一个 TargetException: Non-static method requires a target :/ - JuHwon
我确实修好了!!你的答案是关键。似乎查询解析器甚至无法使用我的布尔标志操作,如果它是真的..似乎我必须写2次几乎相等的查询。 :/你知道更好的解决方案吗? - JuHwon


我遇到了和你一样的问题 newAreaItem == null 是真的。

问题来自LINQ中使用的项不能为空的事实。因此,何时 newAreaItem == null 是的,这意味着 newAreaItem 为null,这会导致抛出错误。

在我看来,你所能做的就是检查后 newAreaItem == null,将newAreaItem设置为该类型的新空对象if newAreaIteam 一片空白。该 newAreaItemIsNull 条件仍然存在,因此

(cm.AreaItem != null && cm.AreaItem.Id == newAreaItem.Id)

在下面的代码中仍然不会被评估 newAreaItem 一片空白。

context.CharacteristicMeasures.
                                 FirstOrDefault(cm => cm.Charge == null &&
                                                      cm.Characteristic != null && cm.Characteristic.Id == c.Id &&
                                                      cm.Line != null && cm.Line.Id == newLine.Id &&
                                                      cm.ShiftIndex != null && cm.ShiftIndex.Id == actShiftIndex.Id &&
                                                      (newAreaItem == null ||
                                                       (cm.AreaItem != null && cm.AreaItem.Id == newAreaItem.Id)));

0
2017-12-05 08:29