상당히 기본적인 부모 - 자녀 관계 설정이 있습니다. 최종 결과는 ASP.NET MVC WebAPI를 통해 결과 테이블을 JSON으로 반환 할 수 있기를 원합니다. Entity Framework 5.0 베타 2를 사용하고 있습니다.
간단한 예제를 통해 오류가 있음을 보여줄 수 있습니다. 클래스의 주어 Category
및 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
포함되도록 열 및 / 또는 쿼리를 설정하는 올바른 방법은 무엇입니까?
자식 컬렉션 속성은 IEnumerable<T>
아닌 ICollection<T>
으로 선언되어야합니다.
또한 명시 적으로 CategoryId
필드를 자식 클래스에 추가 할 필요가 없습니다. EF는 자동으로 데이터베이스에 생성합니다.