코드에 3 개의 엔티티 (EntityA, EntityB, EntityC)가 있고 데이터베이스에 각각의 테이블 (TableA, TableB, TableC)이 있습니다. 또한 세 개의 ID 열 (TableA_ID, TableB_ID, TableC_ID)이있는 기존 조인 테이블이 있습니다.EF 4.1 Fluent API. 세 개의 id 컬럼을 가진 기존 조인 테이블을 사용하면서 두 개의 엔티티를 매핑하는 방법은 무엇입니까?
MODELS:
public class EntityA
{
public Guid EntityA_ID { get; set }
.....
// Each EntityA can be associated with 0 or Many EntityB
public virtual ICollection<EntityB> EntityBCollection { get; set; }
}
public class EntityB
{
public Guid EntityB_ID { get; set; }
.....
// Each EntityB can be associated with 0 or Many EntityA
public virtual ICollection<EntityA> EntityACollection { get; set; }
// Each EntityB can be assocated with 0 or Many EntityC,
// but it becomes 0 or 1 when EntityB is associated with an EntityA
public virtual EntityC EntityC { get; set; }
}
public class EntityC
{
public Guid EntityC_ID { get; set; }
......
// Each EntityC an only be associated with a EntityB
// an EntityC does not exist on its own
public virtual EntityB EntityB { get; set; }
}
DATA CONTEXT:
modelBuilder.Entity<EntityB>()
.HasOptional(entityb => entityb.EntityC)
.WithRequired(entityc => entityc.EntityB)
.Map(map =>
{
map.ToTable("ThreeIDColumnJoinTable").MapKey(new string[]{"EntityA_ID", "EntityB_ID", "EntityC_ID"});
});
나는 다음과 같은 오류 점점 계속 : 다음과 같이
코드에서, 엔티티는 관련이 나는 데이터 컨텍스트에서 매핑을 다시 구성 할 수있는 방법에
Unable to determine the principal end of an association between the types 'EntityC' and 'EntityB'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.
어떤 아이디어를 너무 오류가 발생하지 않으며 ThreeIDColumnJoinTable에 지정된 EntityA의 관계도 포함됩니다.
조인 테이블 항목에 대한 엔티티가 없습니까? 또한 마지막 줄에서'Map' 메소드를 오용하고 있다고 생각합니다. 이 버전은 외래 키 열 이름 만 매핑합니다 (Intellisense의 설명 참조). – Slauma
Slauma, 이전에 (두 개의 열이있는 다 대다 관계의 경우) 이전에 조인 테이블에 대한 엔티티를 작성하지 않았습니다. 또한, "지도"코드는 제가 생각할 수있는 모든 다른 사전 테스트에서의 마지막 시도였습니다. 조인 테이블 엔티티를 만들어 다른 것을 확인해 보겠습니다. 고마워. – fuzzlog
예, 다 대다 조인 테이블의 경우 엔티티가 필요하지 않습니다. 그러나이 테이블은 합성 키를 형성하고 관련 테이블에 대한 FKs 인 정확한 두 열을 가져야합니다. 다른 종류의 조인 테이블은 허용되지 않습니다. – Slauma