C# 此上下文中仅支持原始类型或枚举类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15211362/
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
Only primitive types or enumeration types are supported in this context
提问by RSolberg
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.
我已经看到很多关于这个主题的问题,但我无法对其中的任何一个问题进行排序,这些问题实际上解决了我所看到的问题。我有一个活动实体,用于跟踪将其分配给哪个员工以及哪个员工创建并更新了记录。如果我删除 `where a.AssignedEmployee == currentUser' 代码行,我不会收到下面的运行时错误。
Unable to create a constant value of type 'DataModels.Employee'. Only primitive types or enumeration types are supported in this context.
无法创建类型为“DataModels.Employee”的常量值。在此上下文中仅支持原始类型或枚举类型。
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>
..........
采纳答案by D Stanley
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:
我的猜测是该错误表明 EF 无法将相等运算符转换为Employee
SQL(无论您是假设引用相等还是重写==
运算符)。假设Employee
该类具有唯一标识符,请尝试:
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());
回答by Corey Adler
It doesn't like the fact that you're trying to convert a whole object equality into a database query. You can only do entity framework queries using constant values, much like how you would do SQL queries. The way to solve this would be to compare the IDs to see if the AssignedEmployee's ID is the same as the current user's ID in the employee table.
它不喜欢您尝试将整个对象相等性转换为数据库查询的事实。您只能使用常量值执行实体框架查询,这与执行 SQL 查询的方式非常相似。解决这个问题的方法是比较 ID 以查看 AssignedEmployee 的 ID 是否与员工表中当前用户的 ID 相同。
As a side note, if the currentUser
object that you're tracking isn't of the Employee type you might want to consider caching the corresponding Employee record of that user to better be able to reference it in later queries. That would be a lot better than trying to go through that table constantly. (Again this would only affect you if it is, in fact, in a different table)
附带说明一下,如果currentUser
您要跟踪的对象不是 Employee 类型,您可能需要考虑缓存该用户的相应 Employee 记录,以便在以后的查询中更好地引用它。这比不断尝试通过该表要好得多。(同样,如果它实际上在不同的表中,这只会影响您)
回答by rmiesen
The problem with using == and obj.Equals is that Entity Framework doesn't know how to translate that method call into SQL---even if you overload those two methods into something that would translate into SQL. What you can do to fix this shortcoming in Entity Framework is to create a method that returns an Expression Tree that does the more complex equality checking you are wanting to do.
使用 == 和 obj.Equals 的问题是实体框架不知道如何将该方法调用转换为 SQL ---即使您将这两个方法重载为可以转换为 SQL 的方法。您可以做些什么来修复 Entity Framework 中的这个缺点,即创建一个返回表达式树的方法,该方法执行您想要执行的更复杂的相等性检查。
For example, let's say we have the following class
例如,假设我们有以下类
public class Person {
public string Firstname { get; set; }
public string Lastname { get; set; }
}
In order to return a custom equality operation that Entity Framework can understand, add the following method to the Person class:
为了返回一个 Entity Framework 可以理解的自定义相等操作,将以下方法添加到 Person 类:
public static Expression<Func<Person, bool>> EqualsExpressionTree( Person rhs )
{
return ( lhs ) => string.Equals( lhs.Firstname, rhs.Firstname ) &&
string.Equals( lhs.Lastname, rhs.Lastname );
}
In your LINQ queries, you can leverage your custom equality code like so:
在您的 LINQ 查询中,您可以像这样利用您的自定义相等代码:
Person anotherPerson = new Person { Firstname = "John", Lastname = "Doe" }
personCont.Where( Person.EqualsExpressionTree(anotherPerson) );
//...
if ( personCont.Any( Person.EqualsExpressionTree(anotherPerson)) ) {
//...
Furthermore, the EqualsExpressionTree method can be rewritten to call a static Equals method, allowing you to leverage your custom equality logic. However, in all things, remember that your code must translate to a SQL expression, since we are, after all, calling to a database and not accessing stuff from memory.
此外,可以重写 EqualsExpressionTree 方法以调用静态 Equals 方法,从而允许您利用自定义相等逻辑。但是,在所有事情中,请记住您的代码必须转换为 SQL 表达式,因为毕竟我们是调用数据库而不是从内存中访问内容。