C# EF 非静态方法需要一个目标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15498981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 17:00:10  来源:igfitidea点击:

EF Non-static method requires a target

c#linqentity-framework

提问by JuHwon

I've serious problems with the following query.

以下查询存在严重问题。

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

I get a TargetException: Non-static method requires a targetwhen newAreaItem is null. If newAreaItem is not null I get an NotSupportedException: Unable to create a constant value of type 'PQS.Model.AreaItem'. Only primitive types or enumeration types are supported in this context.

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

Things I've already checked if they're null: c, newLine, actShiftIndex all 3 variables are not null and the Id is accessible.

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

I dont get it... please help.

我不明白……请帮忙。

If u need more information.. dont hesitate to ask...

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

UPDATE

更新

I could eliminate the NotSupportedException, but I still got the TargetException when my newAreaItemIsNull is true.. :/

我可以消除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)));

UPDATE

更新

I finally did it. It seems that the query parse can't parse my newAreaItem(IsNull)because it's not in the DB model somehow !? I have to split my queries..

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

Does someone know a better solution?

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

采纳答案by alex

Try moving newAreaItem == nulloutside of the query

尝试移出newAreaItem == null查询

bool newAreaItemIsNull = (newAreaItem == null);

and replace newAreaItem == nullwith newAreaItemIsNullin query.

并替换newAreaItem == nullnewAreaItemIsNull查询。

Query parser can only operate with the objects in the database, and newAreaItem is not one of them.

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

回答by CristisS

I had the exact same problem as you have when newAreaItem == nullis true.

我和你遇到的问题完全一样newAreaItem == null

The problem comes from the fact that the item used in the LINQ cannot be null. Thus, when newAreaItem == nullis true it means that newAreaItemis null and this leads to the error being thrown.

问题来自于 LINQ 中使用的项目不能为空这一事实。因此, when newAreaItem == null为 true 意味着它newAreaItem为空,这会导致抛出错误。

All you can do in my opinion is, after checking newAreaItem == null, to set the newAreaItem to a new empty object of that type if newAreaIteamis null. The newAreaItemIsNullcondition will still be in place, thus the

在我看来,您所能做的就是在检查之后newAreaItem == null将 newAreaItem 设置为该类型的新空对象,如果newAreaIteam为空。该newAreaItemIsNull条件仍将到位,因此,

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

in your code below will still not be evaluated if newAreaItemis null.

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