2016-11-16 70 views
3

我有三個通過外鍵連接的表。我試圖在問題表中插入1行,在另外兩個表中插入兩行。我收到錯誤 「與外鍵約束Insert語句衝突」預先感謝您的幫助立即在實體中插入多個相關表

public void setMultiAnswer() 
{ 
try 
{ 
    string question = "Question 1" 
    responsesList.Add("Answer1"); 
    responsesList.Add("Answer2"); 
    questionResponsesList.Add(false); 
    questionResponsesList.Add(true); 

    using (Entities testEntity = new Entities()) 
    { 
     Question questionObj = new Question(); 
     questionObj.Question1 = question; 
     questionObj.CreatedBy = "test"; 
     questionObj.CreatedDate = DateTime.Now; 

     QuestionRespons questionResponsesObj = new QuestionRespons(); 
     // fill response 
     foreach (var questionResponse in questionResponsesList) 
     { 
      questionResponsesObj.CorrectResponse = questionResponse; 
     } 

     questionObj.QuestionResponses.Add(questionResponsesObj); 

     Response responseObj = new Response(); 

     // fill response 
     foreach (var response in responsesList) 
     { 
      responseObj.Response1 = response; 
      responseObj.CreatedBy = "test"; 
      responseObj.CreatedDate = DateTime.Now; 
     } 
     questionResponsesObj.Response = responseObj; 

     testEntity.Questions.Add(questionObj); 
     testEntity.SaveChanges(); 
    } 
} 
catch (Exception ex) 
{ 
    Console.Write(ex); 
} 
+0

可以顯示模型代碼嗎? – Sampath

+0

謝謝我添加了模型的一部分。 – user6934713

回答

2

這聽起來像你的問題是ID自動生成的。在這種情況下, int questionId = questionObj.QuestionID; 只會在SaveChanges()調用後才起作用。

一般情況下,如果你有一個帶有外鍵的EntitySet,使用導航屬性更容易,而不是自己建立id引用。

Question questionObj = new Question(); 
questionObj.CreatedBy = "Test"; 
questionObj.CreatedDate = DateTime.Now; 

QuestionRespons questionResponsesObj = new QuestionRespons(); 
// fill question response here 
questionObj.QuestionResponses.Add(questionResponseObj); 

Response responseObj = new Response(); 
// fill your response here 
questionResponsesObj.Response = reponseObj; 
// if you do the above in your loop you should be fine 

testEntity.Questions.Add(questionObj); 
testEntity.SaveChanges(); 

要匹配你的例子:

public void setMultiAnswer() 
{ 
    try 
    { 
     string question = "Question 1" 
     responsesList.Add("Answer1"); 
     responsesList.Add("Answer2"); 
     questionResponsesList.Add(false); 
     questionResponsesList.Add(true); 

     using (Entities testEntity = new Entities()) 
     { 
      Question questionObj = new Question(); 
      questionObj.Question1 = question; 
      questionObj.CreatedBy = "Test"; 
      questionObj.CreatedDate = DateTime.Now; 
      testEntity.Questions.Add(questionObj); 

      for (int i = 0; i < responsesList.Count; i++) 
      { 
       // i am not sure about your relation here, but i assume you require one QuestionResponse per response 
       // which is why a moved the line of code 
       QuestionRespons questionResponsesObj = new QuestionRespons(); 
       questionObj.QuestionResponses.Add(questionResponsesObj); 

       Response responseObj = new Response(); 
       responseObj.Response1 = responsesList.ElementAt(i); 
       responseObj.CreatedBy = "Test"; 
       responseObj.CreatedDate = DateTime.Now; 

       if (!string.IsNullOrEmpty(responseObj.Response1)) 
       { 
        questionResponsesObj.Response = responseObj; 
        questionResponsesObj.CorrectResponse = questionResponsesList.ElementAt(i); 
       } 

      } 
      testEntity.SaveChanges(); 
     } 
    } 
    catch (Exception ex) 
    { 
     Console.Write(ex); 
    } 
} 
+0

謝謝。我需要在表中插入Response。而表questionResonses有另外兩個表的外鍵 – user6934713

+0

我更新了excample以包含Response,我不確定你在'QuestionResponses'和'Reponse'之間的關係,這就是爲什麼我沒有在示例中包含for循環的原因。 – jpk

1

變化

int questionId = questionObj.QuestionID; 
testEntity.SaveChanges(); 

您創建了一個新的實例questionObj順序爲

testEntity.SaveChanges(); 
int questionId = questionObj.QuestionID; 

應該有一個默認的ID爲0. 只有在撥打SaveChanges()之後,才應爲ID分配新分配的實際ID值。

因此,這裏發生的是,您記住questionId的默認值爲0而不是真實ID。作爲一個後果,問題與迴應之間的關係總是錯誤的。

+0

這將原子進程拆分爲兩個事務 - 並不好。 –

+0

@GertArnold同意!我也不會這樣實施它。我只是想保持簡短的答案,而不是完全重寫完整的代碼。 –

1

如果我是你,我會刪除這個QuestionResponse表,只保留問題和迴應。使用導航屬性而不是直接設置外鍵。

Question questionObj = new Question 
    { 
     Text = question, 
     CreatedBy = "Test", 
     CreatedDate = DateTime.Now 
    }; 

    foreach(var response in responsesList.Where(x => !string.IsNullOrEmpty(x))) 
    { 
     Response responseObj = new Response 
     { 
      Text = response, 
      IsCorrect = true, 
      CreatedBy = "Test", 
      CreatedDate = DateTime.Now 
     } 

     questionObj.Add(responseObj); 
    } 

    testEntity.Questions.Add(questionObj); 
    testEntity.SaveChanges(); 
+0

如何包含問題答案 – user6934713

+0

不,不能從模型中移除'QuestionResponse'實體,但設置導航屬性而不是FK值是完全正確的! –

+0

我根據您的建議更改了代碼(您可以看到上面的代碼)。但我只輸入一個questionResponse和response而不是兩個。 – user6934713