0

Possible Duplicate:
Linq-to-entities - Include() method not loading

I have something like this

public class Product
{
    public int ProductId { get; set; }
    public string Name { get; set; }

    public int CategoryId { get; set; }
    [ForeignKey("CategoryId")]
    public virtual Category Category { get; set; }
}

public class Category
{
    public int CategoryId { get; set; }
    public string Name { get; set; }
}

In context I have:

public DbSet<Product> Products { get; set; }

But when I try to use it like this:

List<Product> foo = (from p in Context.Products 
                     where p.Category.Name = "something" 
                     select p).ToList();

Product first = foo.First();

I get the value of Product.CategoryId set but null inside Category

Note: I've tried with 'Context.Products.Include("Category")' and doesn't work.

This works (Category is loaded into first):

List<Product> foo = (from p in Context.Products 
                     where p.Category.Name = "something" 
                     select p).ToList();

Product first = foo.First();

List<Category> categories = Context.Categories.ToList();

So do I have to load category everytime? Why is include not working?

Community
  • 1
  • 1
Gustavo
  • 531
  • 6
  • 20
  • `Include("Category")` should load the category. Maybe your real code is still essentially different from the example. Do you see a SQL query with a join? – Gert Arnold Dec 27 '12 at 00:11
  • Gustavo, I don't agree with closing this question because the "duplicate" is about `Include` in combination with `join` and the issue is well over three years old. Are you using a very old version of EF or can you confirm what I asked above? We might vote to reopen your question if it's still an issue. – Gert Arnold Jan 05 '13 at 21:09

0 Answers0