How Do I Fix "The Include path supplied is not valid"?

ef-code-first entity-framework

Question

My parent-child relationship is set up rather simply. In the end, I want to be able to use the ASP.NET MVC WebAPI to deliver the resultant tables as JSON. Entity Framework 5.0 beta 2 is what I'm using.

I can provide a straightforward example to illustrate the mistake I'm encountering. Because of the coursesCategory and Product accompanied with the relevant data context:

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

    public virtual IEnumerable<Product> Products { get; set; }
}

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

    public virtual Category Category { get; set; }
    public virtual int CategoryId { get; set; }
}

public class ProductDataContext : DbContext
{
    public DbSet<Category> Categories { get; set; }
    public DbSet<Product> Products { get; set; }
}

I get the following problem when attempting to execute a query that contains the products:

A specified Include path is not valid. The EntityType 'FooAndBar.Category' 
does not declare a navigation property with the name 'Products'.

The statement to fetch is rather simple:

var everything = dc.Categories
            .Include(c => c.Products);

How should the columns and/or query be put up correctly to ensure that theProducts are included into theCategories ?

1
10
4/6/2012 1:09:38 AM

Accepted Answer

Properties for child collections must be designated as anICollection<T> , not anIEnumerable<T> .

Additionally, you don't have to include aCategoryId field to the child class; EF will populate the database with that information automatically.

35
3/5/2013 4:54:28 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow