2012-01-31 5 views
6

में मैं इस मॉडल और विन्यासइकाई की रूपरेखा पहले कोड - दो क्षेत्रों के संघ एक संग्रह

public class Person 
{ 
    public int? FatherId { get; set; } 
    public virtual Person Father { get; set; } 
    public int? MotherId { get; set; } 
    public virtual Person Mother { get; set; } 
    public virtual List<Person> Childs { get; set; } 

} 
class PersonConfiguration : EntityTypeConfiguration<Person> 
{ 
    public PersonConfiguration() 
    { 
     HasOptional(e => e.Father).WithMany(e => e.Childs) 
       .HasForeignKey(e => e.FatherId); 
     HasOptional(e => e.Mother).WithMany(e => e.Childs) 
       .HasForeignKey(e => e.MotherId); 
    } 
} 

है और जहां प्रकार प्रारंभिक है मैं इस त्रुटि मिलती है।

निर्दिष्ट स्कीमा मान्य नहीं है। त्रुटियां: (151,6): त्रुटि 0040: टाइप Person_Father नामस्थान परीक्षा (एलियास = स्वयं) में परिभाषित नहीं है।

क्या Childs दोनों गुणों (मां आईडी और पिता आईडी) द्वारा संपत्ति को मानचित्र करने का कोई तरीका है?

उत्तर

14

एक ही संग्रह संपत्ति में दो नेविगेशन गुणों को मैप करना संभव नहीं है। यह उपहास दिखता है लेकिन आपके पास दो संग्रह गुण हैं

public class Person 
{ 
    public int? FatherId { get; set; } 
    public virtual Person Father { get; set; } 
    public int? MotherId { get; set; } 
    public virtual Person Mother { get; set; } 
    public virtual List<Person> ChildrenAsFather { get; set; } 
    public virtual List<Person> ChildrenAsMother { get; set; } 
} 

class PersonConfiguration : EntityTypeConfiguration<Person> 
{ 
    public PersonConfiguration() 
    { 
     HasOptional(e => e.Father).WithMany(e => e.ChildrenAsFather) 
       .HasForeignKey(e => e.FatherId); 
     HasOptional(e => e.Mother).WithMany(e => e.ChildrenAsMother) 
       .HasForeignKey(e => e.MotherId); 
    } 
} 
2

धन्यवाद, आपका जवाब बिल्कुल सही है!

इसके अतिरिक्त, मॉडल बिल्डर कोड यहां है यदि कोई भी एरंगा उपयोग की गई कॉन्फ़िगरेशन विधि के बजाय उस विधि का उपयोग कर रहा है।

protected override void OnModelCreating(DbModelBuilder modelBuilder) 
{ 
    modelBuilder.Entity<Person>(). 
    HasKey(i => i.PersonId); 

    modelBuilder.Entity<Person>(). 
    HasOptional(f => f.Father). 
    WithMany(f => f.ChildrenAsFather). 
    HasForeignKey(f => f.FatherId); 

    modelBuilder.Entity<Person>(). 
    HasOptional(m => m.Mother). 
    WithMany(m => m.ChildrenAsMother). 
    HasForeignKey(m => m.MotherId); 
} 
संबंधित मुद्दे