只有原始类型或枚举类型在这种情况下,支持 [英] Only primitive types or enumeration types are supported in this context

查看:128
本文介绍了只有原始类型或枚举类型在这种情况下,支持的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过很多关于这一主题的问题,但我一直无法通过任何人进行排序,实际上解决了我看到的问题。我有一个跟踪哪个员工被分配到还有哪个员工创造的记录和更新它的活动的实体。如果删除了`那里a.AssignedEmployee ==的currentUsercode行,我没有得到以下运行时错误。


  

无法创建类型的常值'DataModels.Employee。只要
  基本类型或枚举类型,在这方面的支持。


控制器

  VAR的查询=从在db.Activities
            其中,a.AssignedEmployee ==的currentUser
            其中,a.IsComplete ==假
            排序依据a.DueDate
            选择一个;
返回查看(query.ToList());

查看

  @model IEnumerable的< D​​ata.DataModels.Activity>
..........


解决方案

我的猜测是错误表明EF不能员工来SQL翻译等号(无论无论你是假定参考平等或重写 == 运营商)。假设员工类有一个唯一的标识符尝试:

  VAR的查询=从在db.Activities
            其中,a.AssignedEmployeeId == currentUser.Id
            其中,a.IsComplete ==假
            排序依据a.DueDate
            选择一个;
返回查看(query.ToList());

I've seen lots of questions on this topic, but I haven't been able to sort through any of them that actually solve the issue I'm seeing. I have an activities entity that tracks which employee it is assigned to as well as which employee created the record and updated it. If I remove the `where a.AssignedEmployee == currentUser' line of code, I don't get the run time error below.

Unable to create a constant value of type 'DataModels.Employee'. Only primitive types or enumeration types are supported in this context.

CONTROLLER

var query = from a in db.Activities
            where a.AssignedEmployee == currentUser
            where a.IsComplete == false
            orderby a.DueDate
            select a;
return View(query.ToList());

VIEW

@model IEnumerable<Data.DataModels.Activity>
..........

解决方案

My guess is that error indicates that EF cannot translate the equality operator for Employee to SQL (regardless of whether you're assuming referential equality or an overridden == operator). Assuming the Employee class has a unique identifier try:

var query = from a in db.Activities
            where a.AssignedEmployeeId == currentUser.Id
            where a.IsComplete == false
            orderby a.DueDate
            select a;
return View(query.ToList());

这篇关于只有原始类型或枚举类型在这种情况下,支持的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆