当前位置: 首页 > 编程语言 > C#

EF非静态方法需要目标共享

时间:2023-04-11 10:54:33 C#

EF非静态方法需要目标我对以下查询有严重的问题。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为null时,非静态方法需要一个目标,TargetException:非静态方法需要一个目标。如果newAreaItem不为null,则会出现NotSupportedException:Unabletocreateaconstantvalueoftype'PQS.Model.AreaItem'。在此上下文中仅支持原始类型或枚举类型。NotSupportedException:无法创建“PQS.Model.AreaItem”类型的常量值。在此上下文中仅支持原始类型或枚举类型。我检查过它们不为空:c、newLine、actShiftIndex所有3个变量都不为空并且可以访问Id。不懂。。。求助。如果您需要更多信息..不要犹豫,问...更新我可以消除NotSupportedException,但当newAreaItemIsNull为真时我仍然得到TargetException..:/boolnewAreaItemIsNull=(newAreaItem==null);varmc=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),因为它不在数据库模型中!我必须分开我的查询..boolnewAreaItemIsNull=(newAreaItem==null);测量特性mc;如果(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);否则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);有谁知道更好的解决方案?尝试移动newAreaItem==nullboolnewAreaItemIsNull=(newAreaItem==null);使用newAreaI在查询外部和查询内部temIsNull替换newAreaItem==null查询解析器只能对数据库中的对象进行操作,newAreaItem不是其中之一。当newAreaItem==null为真时,我遇到了完全相同的问题。问题来自LINQ中使用的项目不能为空的事实。所以当newAreaItem==null为true时,意味着newAreaItem为null,这会导致抛出错误。在我看来,您可以做的是检查newAreaItem==null,如果newAreaIteam为null,则将newAreaIteam设置为该类型的新空对象。newAreaItemIsNull条件仍然存在,因此(cm.AreaItem!=null&&cm.AreaItem.Id==newAreaItem.Id)如果newAreaItem为null,则仍不会在下面的代码中求值。以上就是C#学习教程:EF非静态方法需要共享target的所有内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多加关注——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)));点击右侧联系管理员删除。如需转载请注明出处:

最新推荐
猜你喜欢