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
EF Non-static method requires a target
提问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 target
when 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 == null
outside of the query
尝试移出newAreaItem == null
查询
bool newAreaItemIsNull = (newAreaItem == null);
and replace newAreaItem == null
with newAreaItemIsNull
in query.
并替换newAreaItem == null
为newAreaItemIsNull
查询。
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 == null
is true.
我和你遇到的问题完全一样newAreaItem == null
。
The problem comes from the fact that the item used in the LINQ cannot be null. Thus, when newAreaItem == null
is true it means that newAreaItem
is 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 newAreaIteam
is null. The newAreaItemIsNull
condition 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 newAreaItem
is 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)));