72

I just declared some code-first models for a new project that uses EntityFramework.

public class BlogEntry
{
    public long Id { get; set; }
    public long AuthorId { get; set; }
    public DateTime PublishedStamp { get; set; }
    public string Title { get; set; }
    public string Text { get; set; }

    public virtual User Author { get; set; }
}

public class User
{
    public long Id { get; set; }
    public string Email { get; set; }
    // ...
}

class BlogDb : DbContext
{
    public DbSet<BlogEntry> Entries { get; set; }
    public DbSet<User> Users { get; set; }
}

Now suppose I want to retrieve the 10 most recent blog entries:

var entries = new BlogDb().Entries.OrderByDescending(...).Take(10).ToList();

The problem now is that accessing entry.Author will cause another database query. You wouldn’t want a separate such query for every blog entry. Now, it is my understanding that the purpose of Include is exactly this case, so I can say:

var entries = new BlogDb().Entries.Include(e => e.Author).(...).ToList();

However, that method doesn’t seem to exist. There is only an Include(string), like this:

var entries = new BlogDb().Entries.Include("Author").(...).ToList();

but this is annoying because it’s not compile-time checked and will be missed by the rename refactoring. Surely the version with the lambda is the “correct” approach.

Where did that method go? Is it no longer included in EntityFramework?

(I know that I can write an extension method for myself to achieve this, so you don’t have to. I’d just like to know whether I’m missing something.)

Timwi
  • 65,159
  • 33
  • 165
  • 230

1 Answers1

103
using System.Data.Entity;

It's in EF v4.1 and above, but you need a reference as it is an extension method.


Edit (thanks to @EastonJamesHarvey)

If using EF Core the import should be:

using Microsoft.EntityFrameworkCore;
undefined
  • 33,537
  • 22
  • 129
  • 198
  • Thanks! Out of curiosity though, why is that an extension method and not simply another overload of the original method? – Timwi Mar 25 '12 at 01:05
  • Im actually not sure lol would be a question for the ADO.Net team – undefined Mar 25 '12 at 01:10
  • 6
    It's an extension method on IQueryable so that you can use it anywhere in a LINQ query. If it were defined only on DbQuery or similar, then as soon as you used another method such as .Where, .OrderBy, and so on, you would no longer see the method because the return type of these methods is IQueryable. – Arthur Vickers Mar 25 '12 at 15:20
  • It seems crazy, VS 2012 RC add this reference automatically but I don't know why suddenly it stopped adding it. The reference was missing and it fixed now. – Mohsen Afshin Jul 13 '12 at 07:52
  • 2
    I was confused by the reference to 4.1. Note you do *NOT* need to reference EF 4.1 in order to access the desired Include extension method. It's a method of the `System.Data.Entity.QueryableExtensions` object also provided in EF 6.0.2 (the latest as of this writing). You just need to import the right namespace in order to access it. – BlueMonkMN Mar 19 '14 at 17:51
  • 1
    With netcoreapp1.0 it's now: using Microsoft.EntityFrameworkCore; – Easton James Harvey Dec 08 '16 at 19:49