2012-03-29 15 views
0

Ich muss einige Datensätze in der Tabelle "StaffSectionInCharge" hinzufügen, es hat nur zwei Spalten StaffId und SectionId, ich habe Werte von StaffId und StudentId ..... das Problem ist, ich kann nicht hinzufügen Aufzeichnungen direkt auf diese Tabelle ..... ist Entity Framework und das Design dieser Tabelle sind mitSpeichern von Werten in der Datenbank

[EdmRelationshipNavigationPropertyAttribute("Model", "StaffSectionInCharge", "Section")] 
    public EntityCollection<Section> Sections 
    { 
     get 
     { 
      return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedCollection<Section>("Model.StaffSectionInCharge", "Section"); 
     } 
     set 
     { 
      if ((value != null)) 
      { 
       ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedCollection<Section>("Model.StaffSectionInCharge", "Section", value); 
      } 
     } 
    } 

ich diese Tabelle durch Mitarbeiter Tabelle zugreifen muß, habe ich versucht, wie

DataAccess.Staff staff = buDataEntities.Staffs.First(s=>s.StaffId==StaffId); 
       staff.Sections.Add(); 

am stucking hier, und kann nicht weiter bewegen, kann mir hier jemand helfen

Antwort

1

können Sie versuchen:

Staff staff = buDataEntities.Staffs.First(s => s.StaffId == StaffId); 
Section section = buDataEntities.Sections.First(s => s.SectionId == SectionId); 

staff.Sections.Add(section); 

buDataEntities.SaveChanges(); 

Oder (die die zweite Abfrage an die Datenbank nicht benötigt):

Staff staff = buDataEntities.Staffs.First(s => s.StaffId == StaffId); 

Section section = new Section { SectionId = SectionId }; 
buDataEntities.Sections.Attach(section); 

staff.Sections.Add(section); 

buDataEntities.SaveChanges(); 

Oder (die überhaupt in der Datenbank keine Abfrage benötigt):

Staff staff = new Staff { StaffId = StaffId }; 
buDataEntities.Staffs.Attach(staff); 

Section section = new Section { SectionId = SectionId }; 
buDataEntities.Sections.Attach(section); 

staff.Sections.Add(section); 

buDataEntities.SaveChanges(); 
+0

groß, seine Arbeits ........... dank alottt – shanish

+0

wie kann ich überprüfen, ob die Id, die in der Datenbank ist schon da sind vorbei ...... kann u Hilf mir – shanish

+0

@ Shanish: Könnten Sie das als neue Frage stellen? Es ist schwieriger, hier im Kommentarbereich zu antworten. – Slauma

Verwandte Themen