Only parameterless constructors and initializers are supported in LINQ to Entities
近期开发过程中,使用Entity Framework时,在对一个实体通过主键(GUID类型)查询时,出现“Only parameterless constructors and initializers are supported in LINQ to Entities”错误,代码如下:
var roles = from r in entites.Roles
where r.RoleId == new Guid(roleId)
select r;
而var roles = from r in entites.Roles
where r.RoleId == new Guid(“fe3f46fd-b85a-42f5-a490-fd76e249f045”)
select r;
这样则没有问题,我仔细对比了roleId的值,实在是没发现问题,最终google出两篇文章还算可以:
链接2指出问题的关键:有些CLR方法当前并不能转化为规范的表达式树方法(翻译的不当:Certain CLR methods are converted to command tree canonical functions, which can be executed on the database. If a CLR method cannot be mapped to a command tree canonical function, an exception will be thrown when translation occurs.),反正内意思就是有些不支持,就是不能用,哈哈。
微软提供了一个可用CLR方法的列表《CLR Method to Canonical Function Mapping》,链接: