无法将lambda表达式转换为类型'string'当使用Include方法在EF中急切加载实体时
本文关键字:Include 方法 EF 实体 加载 表达式 lambda 转换 string 类型 | 更新日期: 2023-09-27 18:10:07
在将其移动到自己的类之前,我在控制器中使用了以下方法,并且它抱怨s => s.LocationAssignment
声明它不能从lambda更改为string,因为它是委托。
public static IEnumerable<Service> getAllService()
{
using (var db = new LocAppContext())
{
var service = db.Locations.Include(s => s.LocationAssignment);
var serv = (from s in db.Services
where s.active == true
select s).ToList();
return serv;
}
}
如果您使用的是EF版本4,那么Include
方法不接受lambda表达式,而是接受字符串。这个错误对于您的特定情况是非常有意义的。这是EF 4中Include
方法的实际定义:
public DbQuery Include(string path)
那么你可以这样做:
db.Locations.Include("LocationAssignment");
只是想补充一下,我收到了这个错误。我错过了下面的引用,花了我一段时间才明白,因为我本以为错误是指缺少一个命名空间指令或其他东西,而不是委托不能转换为字符串。
using System.Data.Entity;
不使用service
service
的类型为IQueryable<Locations>
。方法返回IEnumerable<Service>
public static IEnumerable<Service> getAllService()
{
using (var db = new LocAppContext())
{
// var service = db.Locations.Include(s => s.LocationAssignment);
var serv = (from s in db.Services
where s.active == true
select s).ToList();
return serv;
}
}