1

시험 및 기타 자료 용 앱을 개발 중입니다 ... 내 앱에 질문을 추가 한 다음 추가 할 수 있습니다. 시험. 나는 또한 코드 우선을 사용하고 있습니다.모델 목록을 업데이트하려고 할 때 오류가 발생했습니다 : PRIMARY KEY 제약 조건 위반

public class Question 
{ 
    public Question() { 
     this.Tests = new HashSet<Test>(); 
    } 

    public int QuestionId { get; set; } 
    [Display(Name = "Descripción")] 
    public string QuestionDescription { get; set; } 
    [Display(Name = "Competencia")] 
    public int ProfiencyId { get; set; } 

    public virtual Proficiency Profiency { get; set; } 
    public virtual ICollection<Test> Tests { get; set; } 
} 

그리고 내 테스트 모델 :

public class Test 
{ 
    public Test() { 
     this.Questions = new HashSet<Question>(); 
    } 

    public int TestId { get; set; } 
    [Display(Name = "Descripción")] 
    public string TestDescription { get; set; } 
    [Display(Name = "Tipo de evaluación")] 
    public EVALUATE_TO EvaluateTo { get; set; } 

    public ICollection<Question> Questions { get; set; } 
} 

나는 많은 관계로 많은 유창함 API를 사용

는 여기에서 우리는 내 질문 모델이 있습니다.

그리고이 방법으로 테스트 질문을 업데이트합니다.

private void UpdateTestQuestions(string[] selectedQuestions, Test testToUpdate) 
    { 
     if (selectedQuestions == null) 
     { 
      testToUpdate.Questions = new List<Question>(); 
      return; 
     } 

     var selectedQuestionsHS = new HashSet<string>(selectedQuestions); 
     var testQuestions = new HashSet<int> 
      (testToUpdate.Questions.Select(c => c.QuestionId)); 
     foreach (var question in ApplicationDbContext.Questions) 
     { 
      if (selectedQuestionsHS.Contains(question.QuestionId.ToString())) 
      { 
       if (!testQuestions.Contains(question.QuestionId)) 
       { 
        if (!testToUpdate.Questions.Contains(question)) { 
         testToUpdate.Questions.Add(question); 
        } 
       } 
      } 
      else 
      { 
       if (testQuestions.Contains(question.QuestionId)) 
       { 
        testToUpdate.Questions.Remove(question); 
       } 
      } 
     } 
    } 

그리고 데이터베이스를 응용 프로그램 중단을 저장하려고하고 내가 오류 같은 얻을 때 : PRIMARY KEY 제약 조건 'PK_dbo.QuestionTest'의

위반. 'dbo.QuestionTest'개체에 중복 키를 삽입 할 수 없습니다. 중복 키 값은 (1, 1)입니다.

내가있는 마이크로 소프트의 공식 문서를 다음이었다 Microsoft Documentation MVC 5 Updating

그리고 그들은 우리가 그의 멤버를 변경하여 목록을 수정하고 저장할 수 있다고,하지만 난 오류가 ... 사람은 이유를 알고 ? 읽어 주셔서 감사합니다. 너희들이 나를 도울 수 있기를 소망한다.

답변

0

모든 연구와 독서를하고 많은 것을 시도한 후에 ... 난 그저 미시시피 목록이 가상이 아니기 때문에 문제가되었습니다. 누군가가 똑같은 고통을 겪으면 대답합니다 ...