2014-06-05 3 views
0

나는 다음과 같은 관계를 설명 할 필요가 엔티티 프레임 워크와 같은 테이블의 레코드 간의 관계를 설명하는 방법 :EF에서

headquarter   <= main location 
     plant  ----+ 
     warehouse  | 
     store-1  +----> child Locations 
     store-2  | 
     store-n ----+ 

그래서 나는 mainLocationID 필요 같은

기업이 많은 위치를 가질 수를 나는 아이의 위치를 ​​주어진 모든 하위 위치

2

)에 액세스 할 수있는 위치 모델에서, 나는

1) 주요 위치를 제공 할 수 있도록 내가 찾을 수 있습니다 그것의 주요 위치. 나는 내가 얻을 위치 클래스의 컨트롤러를 스캐 폴딩하려고으로하기 때문에

public class myappContext : DbContext 
{ 
    public myappContext() : base("myappContext") 
    { 
    } 

    public DbSet<Location> Locations { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 

     modelBuilder.Entity<Location>() 
          .HasOptional(l => l.ChildLocations) 
          .WithMany() 
          .HasForeignKey(l => l.mainLocationID); 
    } 
} 

지금 내가 붙어있어

public class Location 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public string type { get; set; } 
    public bool flagMainLocation { get; set; } 

    public int? mainLocationID { get; set; } 
    public virtual ICollection<Location> ChildLocations { get; set; } 
} 

내 dbcontext에서 :

그래서 나는 다음을 수행하려고 다음 오류가 발생했습니다.

"myapp.DAL.Location_ChildLocations:: Multiplicity conflicts with 
the referential constraint in Role 'Location_ChildLocations_Target' 
in relationship 'Location_ChildLocations'. Because all of the 
properties in the Dependant Role are non-nullable, multiplicity 
of the Principal Role must be '1'." 

이 오류 메시지의 암호를 해독하려면 전문가가 아닙니다. 이 구성의 문제점을 알려주는 사람이 있습니까?

나는 또한 당신이하려고하면 어떻게 이런 식으로

Location myChildLocation = db.Locations.Find(some_location_id); 
Location mainLocation = myChildLocation.mainLocation; 

답변

1

의 주요 위치를 얻을 수 있도록하고 싶습니다 : BiffBaffBoff에서 힌트,

modelBuilder.Entity<Location>() 
    .HasMany(l => l.ChildLocations) 
    .WithOptional() 
    .HasForeignKey(l => l.mainLocationID); 
+0

정확히 같은 오류 메시지. 종속성 역할의 모든 속성이 nullable ...이 아니기 때문에 왜 그런지 이해할 수 없습니다. 외래 키는 null 가능하고 모든 문자열 속성은 nullable이며 bool 속성 만 nullable이 아닙니다 .... – kranz

+0

완료 - 이에 대한 완전한 대답을 게시했습니다. 많은 감사합니다. – kranz

1

확인하고 어떤 구문을 추가 주요 위치에 도달하기위한 설탕 나는 마침내 달리고있다 :

public class Location 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
    public string type { get; set; } 
    public bool flagMainLocation { get; set; } 

    public int? mainLocationID { get; set; } 
    public virtual ICollection<Location> ChildLocations { get; set; } 
    public virtual Location mainLocation { get; set;} 
} 

public class myappContext : DbContext 
{ 
    public myappContext() : base("myappContext") 
    { 
    } 

    public DbSet<Location> Locations { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Conventions.Remove<PluralizingTableNameConvention>(); 

     modelBuilder.Entity<Location>() 
      .HasMany(l => l.ChildLocations) 
      .WithOptional() 
      .HasForeignKey(l => l.mainLocationID); 
    } 
} 

g ithub https://github.com/kranz/selfRefModel