EF를 통해 다 대다 테이블을 어떻게 읽습니까? 다 대다 테이블을 사용하는 방법을 모르겠습니다. 의 말을 보자 Product_Category
는 어디서 얻었 ProductID
하고 CategoryID
.
예를 들어 어떻게 액세스 할 수 있습니까?
using(Entities db = new Entities)
{
/* cant access these here.. */}
방법?? 그러나 도달 할 수 있지만 또는 Product_Category
액세스 할 수 없습니다 .ProductID
CategoryID
모든 제품을 나열하고 싶습니다 Product_Category.CategoryID == Category.ID
.
이전에 다 대다 테이블을 사용한 적이 없기 때문에 asp.net에서 EF를 통해 테이블에 액세스하는 방법에 대한 몇 가지 간단한 예제에 감사드립니다.
감사
탐색 속성은 여기에서 친구입니다. 접합 테이블에 다른 속성이 없으면 필요하지 않습니다. 이것이 모델에 Product_Category가없는 이유입니다. 따라서 모델은 다음과 같습니다.
public class Product
{
public Product()
{
this.Categories = new HashSet<Category>();
}
public int ProductId { get; set; }
public string ProductName { get; set; }
public virtual ICollection<Category> Categories { get; set; }
}
public class Category
{
public Category()
{
this.Products = new HashSet<Product>();
}
public int CategoryId { get; set; }
public string CategoryName { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
이제 한 카테고리의 모든 제품을 원하면 다음과 같이 할 수 있습니다.
var productsInCategory = db.Categorys
.Where(c => c.CategoryId == categoryId)
.SelectMany(c => c.Products);
명시적인 접합 테이블을 원한다면 https://lostechies.com/jimmybogard/2014/03/12/avoid-many-to-many-mappings-in-orms/를 참조하십시오.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다