2010-12-14 3 views
1

거기에 간단한 모델 :CTP5 : 자체 참조 문제?

public class Node 
{ 
    public long Id { get; set; } 
    public virtual Node Parent { get; set; } 
} 

다음지도 코드 예외를 throw합니다 :

자체 참조 사용자 지정 외부 키 이름을 수행 할 수없는 수 있습니까?

+0

입니다 그래서 내 Node 클래스는 다음과 같습니다? Linq2Entities는 추측 일뿐입니다 ... – leppie

+0

던져진 예외는 무엇입니까? – Paul

답변

1

나는 똑같은 일을하지만 EF가 올바른 키를 얻는 것에 의존하지 않고 직접 해낸다.

public class Node 
{ 
    public int Id {get;set;} 
    public int ParentId {get;set;} 

    public virutal Node Parent {get;set;} 
    public virtual ICollection<Node> Children {get;set;} 
} 

그리고 다음과 같이 다음 모델 빌더는 설정은 다음과 같습니다 : CTP5 무엇

builder.Entity<Node>().HasKey(x => x.Id); 

builder.Entity<Node>() 
    .HasOptional(s => s.Parent) 
     .WithMany(c => c.Children) 
      .HasForeignKey(s => s.ParentId); 

builder.Entity<Node>().ToTable("Node");