2012-02-17 43 views
0

我需要使用MVC和實體框架插入記錄的幫助。我有一個動態創建的表單,可以包含很多問題。編輯時,我想刪除現有的答案(它成功)並插入新的答案。MVC實體框架 - 插入數據 - ReferentialConstraint中的依賴屬性映射到商店生成的列

我收到以下錯誤:
不能在表中插入的標識列的顯式值「tblModeratorReportAnswers」當IDENTITY_INSERT設置爲OFF。

如果我在的DbContext類
modelBuilder.Entity<QuestionAnswer>().Property(p => p.AnswerID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);添加以下行我得到這個錯誤:
在ReferentialConstraint從屬屬性映射到一個存儲生成列。列:'AnswerID'。

這裏是我的代碼是做更新

// 
// POST: /Home/Edit/1 
[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Edit(FormCollection formCollection, int moderatorReportId) 
{ 
    ModeratorReport reportToEdit = repository.GetModeratorReportById(moderatorReportId); 
    List<QuestionAnswer> originalReportAnswers = repository.GetAllModeratorReportAnswers(moderatorReportId).ToList(); 

    foreach (QuestionAnswer answer in originalReportAnswers) { 
     repository.DeleteAnswer(answer); 
    } 

    repository.Save(); 

    int sectionID; 
    int questionID; 

    foreach (string key in formCollection.AllKeys) 
    { 
     var value = formCollection[key.ToString()]; 

     Match m = Regex.Match(key, "section(\\d+)_question(\\d+)"); 

     if (m.Success) { 
      QuestionAnswer newAnswer = new QuestionAnswer(); 

      sectionID = Convert.ToInt16(m.Groups[1].Value.ToString()); 
      questionID = Convert.ToInt16(m.Groups[2].Value.ToString()); 

      newAnswer.ModeratorReportID = moderatorReportId; 
      newAnswer.QuestionID = questionID; 
      newAnswer.Answer = value; 
      repository.AddAnswer(newAnswer); 
     } 
    } 

    repository.Save(); 

    reportToEdit.Status = "SUBJECTOFFICER SAVED"; 

    AuditItem auditItem = new AuditItem(); 
    auditItem.ModeratorReportID = moderatorReportId; 
    auditItem.Status = "SUBJECTOFFICER SAVED"; 
    auditItem.AuditDate = DateTime.Now; 
    auditItem.Description = "The Moderator report ID: " + moderatorReportId + " was saved."; 
    auditItem.UserID = User.Identity.Name; 

    db.Audit.Add(auditItem); 

    repository.Save(); 

    return RedirectToAction("Details", new { id = moderatorReportId }); 
} 

...在我的倉庫

// 
// Persistance 

public void Save() 
{ 
    db.SaveChanges(); 
} 

public void AddAnswer(QuestionAnswer answer) 
{ 
    db.Answers.Add(answer); 
    Save(); 
} 

public void DeleteAnswer(QuestionAnswer answer) 
{ 
    db.Answers.Attach(answer); 
    db.Answers.Remove(answer); 
} 

我還檢查了所有我的主鍵,外鍵和他們都OK 。主鍵全部設置爲'身份'。

我一直試圖整理這個問題整天。我不知道該怎麼做來解決它。如果任何人都可以給我任何建議,這將非常感激。

回答

1

也許這是我對ASP.NET MVC和實體框架的經驗不足,但我現在通過改變我更新報表的邏輯來解決這個問題。

而不是刪除答案並重新插入它們。我現在檢索答案並將答案屬性更改爲新答案。然後使用db.SaveChanges()。

// 
// POST: /Home/Edit/1 
[AcceptVerbs(HttpVerbs.Post)] 
public ActionResult Edit(FormCollection formCollection, int moderatorReportId) 
{ 
    ModeratorReport reportToEdit = repository.GetModeratorReportById(moderatorReportId); 
    List<QuestionAnswer> originalReportAnswers = repository.GetAllModeratorReportAnswers(moderatorReportId).ToList(); 

    int sectionID; 
    int questionID; 

    foreach (string key in formCollection.AllKeys) 
    { 
     var value = formCollection[key.ToString()]; 

     Match m = Regex.Match(key, "section(\\d+)_question(\\d+)"); 

     if (m.Success) { 
      QuestionAnswer newAnswer = new QuestionAnswer(); 

      sectionID = Convert.ToInt16(m.Groups[1].Value.ToString()); 
      questionID = Convert.ToInt16(m.Groups[2].Value.ToString()); 

      foreach(QuestionAnswer answerToEdit in originalReportAnswers) { 
       if (answerToEdit.QuestionID == questionID) 
       { 
        answerToEdit.Answer = value; 
       } 
      } 
     } 
    } 

    repository.Save(); 

    reportToEdit.Status = "SAVED"; 

    AuditItem auditItem = new AuditItem(); 
    auditItem.ModeratorReportID = moderatorReportId; 
    auditItem.Status = "SAVED"; 
    auditItem.AuditDate = DateTime.Now; 
    auditItem.Description = "The Moderator report ID was saved."; 
    auditItem.UserID = User.Identity.Name; 

    db.Audit.Add(auditItem); 

    repository.Save(); 

    return RedirectToAction("Details", new { id = moderatorReportId }); 
} 
0

Cannot insert explicit value for identity column in table 'tblModeratorReportAnswers' when IDENTITY_INSERT is set to OFF.

此錯誤說明您將值顯式插入自動生成的列(標識列)。

A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'AnswerID'.

此錯誤說,存在這樣的情況自動生成AnswerID被視爲FK一些錯誤配置的關係 - 不支持。 IdentityComputed屬性不得是FK。

+0

感謝您的回覆。我不明白爲什麼我會得到第一個錯誤,因爲我沒有在代碼中的任何位置設置「AnswerID」。如果我在遍歷代碼時將鼠標懸停在屬性上,它的值爲0.這是否會導致問題?在尋找解決方案後,我只在我的DbContext中添加了該行,並建議將其作爲可能的解決方案。 – 2012-02-20 09:54:48

+1

我已將任何標識列作爲外鍵刪除,並且仍然收到錯誤消息。你有什麼可能的想法。我完全被這一個難住了。 – 2012-02-20 10:13:18

相關問題