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?