开发者

Linq-to-Entities Include method not found

开发者 https://www.devze.com 2023-02-15 18:51 出处:网络
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:

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开发者_如何学JAVA I have enable it somehow?


I was also getting this error and resolved it by adding the following reference:

using System.Data.Entity;


Yes the method is build into EF but it is not available on IQueryable interface. It is available on ObjectQuery. If you want to call it on IQueryable you must create your own extension wich will convert current query to ObjectQuery and perform Include. Something like:

public static IQueryable<T> Include<T>(this IQueryable<T> query, string property)
{
  var objectQuery = query as ObjectQuery<T>;
  if (objectQuery == null)
  {
    throw new NotSupportedException("Include can be called only on ObjectQuery");
  }

  return objectQuery.Include(property);
}

Or you must use Entity Framework Feature CTP5 where such extensions are already available.


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.


I was also facing this issue and resolved it in asp.net core 3.0 project by adding the following reference:

using Microsoft.EntityFrameworkCore; 


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号