Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Non-static method requires a target

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.

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.

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

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

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?

like image 529
JuHwon Avatar asked Mar 19 '13 12:03

JuHwon


1 Answers

Try moving newAreaItem == null outside of the query

bool newAreaItemIsNull = (newAreaItem == null);

and replace newAreaItem == null with newAreaItemIsNull in query.

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

like image 74
alex Avatar answered Oct 21 '22 14:10

alex