2016-07-12 4 views
0

Ich verwende Entitätsrahmencode zuerst, um eine SQL-Datenbank und "SEED" -Methode zu entwerfen, um die Datenbank mit Anfangsdaten zu füllen. Im Folgenden sind zwei Modelle mit einer zu viele Beziehung. "Foo" kann viele "FooSection" habenEntitätsrahmen zusammengesetzter Schlüssel, der doppelte Daten nicht zulässt

public class Foo { 
    [Key] 
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)] 
    public int FooId {get; set;} 
    // Some more properties. 
    // Navigation collection 
    public virtual ICollection<FooSection> fooSections {get; set;} 
} 

public class FooSection { 
    // Has composite key 
    [ForeignKey("foo"), Column(Order=1)] 
    public int FooId {get; set;}  
    [Key, Column(Order=2)] 
    public string SectionName {get; set;} 
    // Some more properties 
    // Navigation property 
    public virtual Foo foo {get; set;} 
} 

Say erste Instanz von "foo" mit FooId = 1, verfügt über 2 "FooSection" und zweiten Instanz von "foo" mit FooId = 2 ist, hat 1 "FooSection" . So sieht meine Samen Methode wie folgt -

protected override void Seed(PDI.Cloud.Models.ApplicationDbContext context) 
{ 
    // First add data for "Foo" 
    var FooList = new List<Foo>(); 
    if(!(context.Foos.Any())) 
    { 
     // First instance of Foo 
     FooList.Add( 
      new Foo{ 
      // Assign values to properties here 
      } 
     ); 
     // Second instance of Foo 
     FooList.Add( 
      new Foo{ 
      // Assign values to properties here 
      } 
     ); 
     FooList.ForEach(f => context.Foos.AddOrUpdate(f)); 
     context.SaveChanges(); 

     // Get info of "FooSection" 
     var fooSectList = getFooSectList(context); 
     // Assign "FooSection"s to respective "Foo". 
     FooList[0].fooSections.Add(fooSectList[0]); // Section 1 for Foo with id = 1 
     FooList[0].fooSections.Add(fooSectList[1]); // Section 2 for Foo with id = 1 
     FooList[1].fooSections.Add(fooSectList[2]); // Section 1 for Foo with id = 2 
     Context.SaveChanges(); 
    } 
} 

private List<FooSection>getFooSectList(PDI.Cloud.Models.ApplicationDbContext context) 
    { 
     var FooSectList = new List<FooSection>(); 
     if(!(context.FooSections.Any())) 
     { 
     // 1st FooSection for Foo with FooId = 1 
     FooSectList.Add( 
      new FooSection{ 
      FooId = 1, 
      SectionName = "Sect1" 
      } 
     ); 
     // 2nd FooSection for Foo with FooId = 1 
     FooSectList.Add( 
      new FooSection{ 
      FooId = 1, 
      SectionName = "Sect2" 
      } 
     ); 
     // 1st FooSection for Foo with FooId = 2 
     FooSectList.Add( 
      new FooSection{ 
      FooId = 2, 
      SectionName = "Sect1" 
      } 
     ); 
     FooSectList.ForEach(f => context.FooSections.AddOrUpdate(f)); 
     context.SaveChanges(); 
     } 
     return FooSectList; 
    } 

Wenn ich versuche, die Samen Methode auszuführen, es gibt ich SQLException „Verletzung von PRIMARY KEY-Einschränkung nicht doppelte Schlüssel in Objekt einfügen kann der doppelte Schlüsselwert (0,.. Abschnitt1) "

Fehle ich hier etwas? Weil ich denke, dass für Composite-Schlüssel, soweit die Kombination einzigartig ist, ich solche Fehler nicht bekommen sollte.

Ich würde jede Hilfe zu schätzen wissen.

Danke.

Antwort

0

Ich denke, Sie erwarten FooSection einen zusammengesetzten Primärschlüssel von FooId, SectionName zu haben. Wie Sie es jetzt haben, ist Ihr Primärschlüssel jedoch nur SectionName.

Sie finden das Key-Attribut auf die FooId Eigenschaft hinzufügen, um sie in einen zusammengesetzten Primärschlüssel drehen:

[Key, ForeignKey("foo"), Column(Order=1)] 
public int FooId {get; set;}  
+0

Ahh! der Schlüssel". Ja, das habe ich vermisst. Danke, Stan. – Akshada

Verwandte Themen