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

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

本文介绍了EF非静态方法需要一个目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有严重的问题,下面的查询。

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



我收到了 TargetException:非静态方法需要一个目标时newAreaItem为空。
如果newAreaItem不为空,我得到一个 NotSupportedException异常:无法创建类型的常值'PQS.Model.AreaItem。只有基本类型或枚举类型在这方面的支持

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.

如果他们是空我已经检查的事情:
C符,换行符actShiftIndex所有3个变量是不是null和ID访问。

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

我不明白它...请帮助。

I dont get it... please help.

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

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

更新

我可以消除引发NotSupportedException ,但我还是得到了TargetException当我newAreaItemIsNull是真的..:/

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

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),因为它在DB模式不是莫名其妙!?
我有分裂我的查询。

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..

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?

推荐答案

尝试移动 newAreaItem == NULL 查询

bool newAreaItemIsNull = (newAreaItem == null);

和替换 newAreaItem == NULL newAreaItemIsNull 查询。

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

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

这篇关于EF非静态方法需要一个目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:45