5

비슷한 질문 : 나는 역할 클래스가 Mapping collection of strings with Entity Framework or Linq to SQL매핑 값 형 수집

:

public class Role 
{ 
    public int RoleID { get; set; } 
    public virtual IList<string> Actions { get; set; } 
} 

나는 열와 DB, "RoleActionMapping"에 매핑 테이블이를 "역할 ID "및"조치 ". "RoleID"는 Role 테이블의 외래 키 포인트이고 "Action"은 varchar입니다. RoleActions로 Role 클래스를 채우기 위해 EF 매핑을 설정하는 것처럼 보이지 않습니다.

달성 방법에 대한 아이디어가 있으십니까? 감사!

답변

3

EF는 이러한 매핑을 제공하지 않습니다. 당신이 그것을 매핑 할 경우

을 사용해야합니다 : 당신은 더 Actions 속성에서 IEnumerable<string> 것이다 내부적으로 선택 데이터를 반환하지 매핑 속성을 provied하는 Role 클래스를 확장 할 수 있습니다

public class Role 
{ 
    public int RoleID { get; set;} 
    public virtual IList<RoleAction> Actions { get; set; } // you should initialize collection 
} 

public class RoleAction 
{ 
    public int ActionId { get; set; } // PK must be defined or entity is readonly 
    public string Action { get; set; } 
    public virtual Role { get; set; } 
} 

.

그러나이 접근법을 따른 후에는 RoleAction 엔티티 사이의 M : N 관계로 모델링해야합니다.

+0

너무 좋았어. 솔루션을 가져 주셔서 감사합니다. – manu08

+0

@ manu08 * 왜 * "값 개체의 컬렉션"이 제공되지 않을지에 대한 견해는 http://mhinze.com/2009/01/10/there-is-never-a-collection-of-value- objects/(이것은 일반적으로 Entity Framework가 아닌 도메인 기반 디자인 용어입니다). – Nathan