未找到 Linq-to-Entities Include 方法
我在 MVC3 应用程序中使用 EF4,并且正在寻找一种方法来查看给定工作组中的所有联系人。在我指定的控制器中:
var wg = from w in _repo.Workgroups.Include("Contact").ToList();
但出现以下错误:
“System.Linq.IQueryable”不包含“Include”的定义,并且找不到接受“System.Linq.IQueryable”类型的第一个参数的扩展方法“Include”(您是否缺少 using 指令或程序集参考?)
我虽然这个方法内置于 EF4 中。我是否以某种方式启用了它?
I am using EF4 within a MVC3 application and I was looking for a way to view all my contacts within a given workgroup. In the controller I specified:
var wg = from w in _repo.Workgroups.Include("Contact").ToList();
but I get the following error:
'System.Linq.IQueryable' does not contain a definition for 'Include' and no extension method 'Include' accepting a first argument of type 'System.Linq.IQueryable' could be found (are you missing a using directive or an assembly reference?)
I though this method was built into EF4. Do I have enable it somehow?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我也遇到了这个错误,并通过添加以下参考解决了它:
I was also getting this error and resolved it by adding the following reference:
是的,该方法内置于 EF 中,但在
IQueryable
接口上不可用。它可以在ObjectQuery
上使用。如果您想在 IQueryable 上调用它,您必须创建自己的扩展,它将当前查询转换为 ObjectQuery 并执行 Include。例如:或者您必须使用 实体框架功能 CTP5,此类扩展已可用。
Yes the method is build into EF but it is not available on
IQueryable
interface. It is available onObjectQuery
. If you want to call it onIQueryable
you must create your own extension wich will convert current query toObjectQuery
and performInclude
. Something like:Or you must use Entity Framework Feature CTP5 where such extensions are already available.
布鲁斯·希尔的回答绝对正确。
只是稍微扩展一下他的答案, Include 方法是 System.Data.Entity 命名空间中的扩展方法。
它不会影响您如何使用它们,但 Include 方法实际上是在名为 DbExtensions 的静态类中定义的。在 msdn.microsoft.com 上,您将看到它们记录在那里,而不是在 System.Linq.IQueryable 下,这使得它们更难找到。
Bruce Hill's answer is absolutely right.
Just to expand on his answer a bit, the Include methods are extension methods in the System.Data.Entity namespace.
It doesn't affect how you use them, but the Include methods are actually defined in a static class named DbExtensions. At msdn.microsoft.com, you'll see them documented there, not under System.Linq.IQueryable, making them just a bit harder to find.
我也遇到了这个问题,并通过添加以下参考在 asp.net core 3.0 项目中解决了它:
I was also facing this issue and resolved it in asp.net core 3.0 project by adding the following reference:
如果此问题来自存储过程,请确保 SP 工作正常。
实体框架将尝试预测存储过程的返回值类型,如果 SP 由于其中的表名称或列无效而无法工作,它将返回一个整数,并且实体框架创建一个概念模式,期望一个整数作为返回值而不是一张桌子。
If this issue is coming from a Stored Procedure, ensure that the SP is working fine.
Entity Framework will try predict the kind of return value from the Stored Procedure, and if SP is not working because of invalid Table name or column within it, it returns an Integer, and the Entity Framework creates a Conceptual Schema expecting an integer as return value instead of a table.