我有一个相当基本的亲子关系设置。最终结果是我希望能够通过 ASP.NET MVC WebAPI 以 JSON 形式返回结果表。我正在使用实体框架 5.0 beta 2。
我可以用一个简单的例子来演示我遇到的错误。鉴于课程Category
and Product
与相应的数据上下文:
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; }
}
当我尝试运行包含产品的查询时,出现以下错误:
A specified Include path is not valid. The EntityType 'FooAndBar.Category'
does not declare a navigation property with the name 'Products'.
获取语句非常简单:
var everything = dc.Categories
.Include(c => c.Products);
设置列和/或查询的正确方法是什么,以便Products
包含在Categories
?
子集合属性必须声明为ICollection<T>
, not anIEnumerable<T>
.
此外,您不需要显式添加CategoryId
子类的字段; EF 将在数据库中自动创建它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)