无法创建常量值类型。(Unable to create a constant value of type . Only primitive types or enumeration types are supported in this context)
我有以下方法进行通用检查,
public virtual bool CheckExist(T entity) { var context = new eTRdataEntities(); IQueryable<T> dbQuery = context.Set<T>(); if (dbQuery.Any(e => e == entity)) { return true; } return false; }但是它返回异常:
无法创建常量值类型。 在此上下文中仅支持原始类型或枚举类型。
请指教,
非常感谢,
I have the following method for a generic check exist,
public virtual bool CheckExist(T entity) { var context = new eTRdataEntities(); IQueryable<T> dbQuery = context.Set<T>(); if (dbQuery.Any(e => e == entity)) { return true; } return false; }However it returns the exception:
Unable to create a constant value of type . Only primitive types or enumeration types are supported in this context.
Please advice,
Many Thanks,
最满意答案
尝试通过接受方法名称旁边的类型T来更改代码,如下所示:
public virtual bool CheckExist<T>(T entity) { var context = new eTRIKSdataEntities(); IQueryable<T> dbQuery = context.Set<T>(); if (dbQuery.Any(e => e == entity)) { return true; } return false; }您可能还希望仅将类型限制为类,如下所示:
public virtual bool CheckExist<T>(T entity) where T : class { var context = new eTRIKSdataEntities(); IQueryable<T> dbQuery = context.Set<T>(); if (dbQuery.Any(e => e == entity)) { return true; } return false; }Try changing the code by taking in the type T next to the method name, like this:
public virtual bool CheckExist<T>(T entity) { var context = new eTRIKSdataEntities(); IQueryable<T> dbQuery = context.Set<T>(); if (dbQuery.Any(e => e == entity)) { return true; } return false; }You may also want to restrict the type to classes only, like this:
public virtual bool CheckExist<T>(T entity) where T : class { var context = new eTRIKSdataEntities(); IQueryable<T> dbQuery = context.Set<T>(); if (dbQuery.Any(e => e == entity)) { return true; } return false; }更多推荐
发布评论