【问题标题】:EF Non-static method requires a targetEF 非静态方法需要一个目标
【发布时间】:2013-03-19 12:02:29
【问题描述】:

我对以下查询有严重问题。

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

当 newAreaItem 为空时,我得到一个 TargetException: Non-static method requires a target。 如果 newAreaItem 不为空,我会得到一个 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 是可访问的。

我不明白...请帮助。

如果您需要更多信息..不要犹豫...

更新

我可以消除 NotSupportedException,但是当我的 newAreaItemIsNull 为真时,我仍然得到 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)));

更新

我终于做到了。似乎查询解析无法解析我的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);

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

【问题讨论】:

标签: c# linq entity-framework


【解决方案1】:

尝试将newAreaItem == null 移到查询之外

bool newAreaItemIsNull = (newAreaItem == null);

并在查询中将newAreaItem == null 替换为newAreaItemIsNull

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

【讨论】:

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

newAreaItem == null 为真时,我遇到了与您完全相同的问题。

问题在于 LINQ 中使用的项目不能为空。因此,当newAreaItem == null 为真时,意味着newAreaItem 为空,这会导致错误被抛出。

在我看来,您所能做的就是在检查newAreaItem == null 之后,如果newAreaIteam 为空,则将newAreaItem 设置为该类型的新空对象。 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)));

【讨论】:

    猜你喜欢
    • 2012-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    相关资源
    最近更新 更多