2012-03-29 3 views
0

"StaffSectionInCharge"테이블에 일부 레코드를 추가해야하는데 StaffId 및 SectionId라는 두 개의 열만 있습니다. StaffId 및 StudentId 값이 있습니다. 문제는 내가 추가 할 수 없습니다. 이 테이블에 직접 기록 엔티티 프레임 워크를 사용하고 있습니다 .....이 테이블의 디자인은 내가 직원 테이블을 통해이 테이블에 액세스 할 필요가데이터베이스에 값을 저장합니다

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

, 나는

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

오전 stucking 같은 시도 여기, 그리고 더 이상 움직일 수없는 사람, 아무도 날 여기 도와 줄 수 없다

답변

1

당신이 시도 할 수 :

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

또는 (데이터베이스에있는 쿼리를 전혀 필요로하지 않는다 : (데이터베이스에 두 번째 쿼리를 필요로하지 않는다)

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

staff.Sections.Add(section); 

buDataEntities.SaveChanges(); 

또는를) :

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

큰, 그 작업은 ........... – shanish

+0

alottt 덕분에 내가 어떻게 ...... 통과하고있는 ID가 데이터베이스에 이미 존재하는지 여부 유를 할 수 있습니다 확인하실 수 있습니다 도와주세요 – shanish

+0

@Shanish :이 질문을 새로운 질문으로 제공해 줄 수 있습니까? 코멘트 섹션에서 대답하기가 더 어렵습니다. – Slauma

관련 문제