उत्तर

10

POCO के लिए ...

class Person 
{ 
    public Guid PersonId { get; set; } 
    public virtual Person Parent { get; set; } 
    public virtual ICollection<Person> Children { get; set; } 
} 

... DbContext में मानचित्रण ...

protected override void OnModelCreating(ModelBuilder modelBuilder) 
{ 
    modelBuilder.Entity<Person>() 
     .HasOptional(entity => entity.Parent) 
      .WithMany(parent => parent.Children) 
      .HasForeignKey(parent => parent.PersonId); 
} 

... आप एक डिफ़ॉल्ट कार्यान्वयन दे देंगे की स्थापना की। आप, टेबल स्पष्ट रूप से नाम बदलने के लिए (और अनेक-से-अनेक संबंध चाहते हैं) कुछ इस तरह में जोड़ने की जरूरत है ...

class Person 
{ 
    public Guid PersonId { get; set; } 
    public virtual ICollection<Person> Parent { get; set; } 
    public virtual ICollection<Person> Children { get; set; } 
} 

protected override void OnModelCreating(ModelBuilder modelBuilder) 
{ 
    ConfigureProducts(modelBuilder); 
    ConfigureMembership(modelBuilder); 

    modelBuilder.Entity<Person>() 
     .HasMany(entity => entity.Children) 
     .WithMany(child => child.Parent) 
     .Map(map => 
     { 
      map.ToTable("PersonPersons"); 
      map.MapLeftKey(left => left.PersonId, "PersonId"); 
      map.MapRightKey(right => right.PersonId, "ChildPersonId"); 
      // For EF5, comment the two above lines and uncomment the two below lines. 
      // map.MapLeftKey("PersonId"); 
      // map.MapRightKey("ChildPersonId"); 
     }); 
} 
[इकाई की रूपरेखा 4 CTP 5 स्वयं की
+0

मैं मैप (मानचित्र => सब कुछ के साथ कहां से कनेक्ट करूं? – Omu

+0

एमएपीएम कॉन्फ़िगरेशन के साथ एमटीएम कॉन्फ़िगरेशन दिखाने के लिए ऊपर संपादित किया गया है। मैप()। मुझे लगता है कि एक व्यक्ति और एक व्यक्ति पर्सन पीओसीओ दोनों को भी परिभाषित करने का एक तरीका हो सकता है। –

+2

नोट: ईएफ 5.0 में, बस 'map.MapLeftKey (left => left .PersonId, "PersonId"); 'to' map.MapLeftKey ("PersonId"); ', और सही कुंजी के लिए ऐसा करें। – deerchao

संबंधित मुद्दे