2012-03-29 4 views
0

Je dois ajouter quelques enregistrements dans la table "StaffSectionInCharge", il a seulement deux colonnes StaffId et SectionId, j'ai des valeurs de StaffId et StudentId ..... le problème est que je ne peux pas ajouter le dossiers directement à cette table ..... je utilise Entity Framework et la conception de ce tableau eststocker des valeurs dans la base de données

[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); 
      } 
     } 
    } 

J'ai besoin d'accéder à ce tableau par tableau personnel, j'ai essayé comme

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

am stucking ici, et ne peux pas aller plus loin, quelqu'un peut-il m'aider ici

Répondre

1

Vous pouvez essayer:

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

staff.Sections.Add(section); 

buDataEntities.SaveChanges(); 

Or (qui n'a pas besoin de la deuxième requête à la base de données):

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(); 

Or (qui n'a pas besoin de requête à la base de données du tout):

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

grande, son travail ........... grâce alottt – shanish

+0

comment puis-je vérifier si l'ID qui je passe est déjà dans la base de données ...... peut u aide moi – shanish

+0

@Shanish: Pourriez-vous poser cette question comme une nouvelle question? Il est plus difficile de répondre ici dans la section des commentaires. – Slauma

Questions connexes