1

Entity Framework 3.5では理解できない問題があります(EF 4を使用すると機能します(EF 4に対応するためにコードをわずかに変更します)が、使用しているサーバーのためにEFを使用する必要があります3.5)。私の ASP.NET アプリでは、ユーザーは単純な真偽テストを受けることができます。ユーザーがテストを完了した後、[送信] ボタンをクリックすると、スコアとパーセント ランクが計算され、データがデータベース (SQL Server 2008R2) に書き戻されます。TestTaker モデルへの書き込みは正常に行われますが、TestTakerAnswer モデルへの書き込みを試みると、次の例外が生成されます。

System.Data.UpdateException: 「ComplianceTestContext.TestTakerAnswers」のエンティティは、「FK_TestTakerAnswers_TestTakers」関係に参加しています。0 件の関連する「TestTakers」が見つかりました。1 人の「TestTakers」が予想されます。

以前に投稿されたこのような質問を見たことがありますが、それでも私の問題を理解できませんでした。次に示すのは、[送信] ボタンが選択されたときの EDM とイベント ハンドラーのサブセットです。これに関するヘルプは非常に高く評価されます。ありがとう!

ここに画像の説明を入力

  protected void wizTest_FinishButtonClick(object sender, WizardNavigationEventArgs e)
  {
     // Score test and display result.
     ///////////////////////////////////
     int score = 0;
     double rank;

     using (var db = new ComplianceTestContext())
     {
        // Get reference to wizard control.
        ContentPlaceHolder cph1 = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
        Wizard wizControl = (Wizard)cph1.FindControl("wizTest");

        // 1. Calculate score.
        var questions = from q in db.TestQuestions
                        orderby q.QuestionNumber
                        select q;

        foreach (var item in questions)
        {
           var rdoBtnList = (RadioButtonList)wizControl.FindControl("rdoQ" + item.QuestionNumber);

           if (item.Answer == (rdoBtnList.SelectedValue == "1"))
           {
              score += item.Weight;
           }
        }

        // ==========================================================================================================================================================

        // 2. Calculate percent rank.
        var testTakers = from tt in db.TestTakers
                         orderby tt.Score
                         select tt;

        int totalScores = testTakers.Count() + 101;  // 101 = 1 + (current score) + 100 (salted number of scores).
        int totalScoresWithGrade;

        if (score >= 277)
        {
           // Get total number of A grades for calculating top x% rank.
           totalScoresWithGrade = testTakers.Count(tt => tt.Score >= 277) + 3;  // 3 = 1 + (current A score) + 2 (salted number of A scores).
        }
        else if (score >= 246 && score <= 276)
        {
           // Get total number of B grades for calculating top x% rank.
           totalScoresWithGrade = testTakers.Count(tt => tt.Score >= 246) + 26;  // 26 = 1 + (current B score) + 23 (salted number of B scores) + 2 (salted number of A scores).
        }
        else if (score >= 215 && score <= 245)
        {
           // Get total number of C grades.
           totalScoresWithGrade = testTakers.Count(tt => tt.Score >= 215 && tt.Score <= 245) + 55;  // 55 = 1 + (current C score) + 54 (salted number of C scores).
        }
        else if (score >= 186 && score <= 214)
        {
           // Get total number of D grades for calculating bottom x% rank.
           totalScoresWithGrade = testTakers.Count(tt => tt.Score <= 214) + 22;  // 22 = 1 + (current D score) + 19 (salted number of D scores) + 2 (salted number of F scores).
        }
        else
        {
           // Get total number of F grades for calculating bottom x% rank.
           totalScoresWithGrade = testTakers.Count(tt => tt.Score <= 185) + 3;  // 3 = 1 + (current F score) + 2 (salted number of F scores).
        }

        rank = Convert.ToDouble(totalScoresWithGrade) / totalScores;

        // ==========================================================================================================================================================

        // 3. Write data to TestTaker model.
        var testTaker = new TestTaker { DateTaken = DateTime.Now, Score = score };
        db.AddToTestTakers(testTaker);
        db.SaveChanges();

        // Retrieve newly inserted row's TestTakerId (identity column) for use in inserting data into TestTakerAnswer model.
        int testTakerId = testTaker.TestTakerId;

        // ==========================================================================================================================================================

        // 4. Write data to TestTakerAnswer model.
        foreach (var item in questions)
        {
           var rdoBtnList = (RadioButtonList)wizControl.FindControl("rdoQ" + item.QuestionNumber);
           var answer = new TestTakerAnswer { AnswerNumber = item.QuestionNumber, TestTakerId = testTakerId, Answer = (rdoBtnList.SelectedValue == "1") };
           db.AddToTestTakerAnswers(answer);
        }

        db.SaveChanges();
     }

     // 5. Display score to user.
     var testScoreMsg = "Your test score is " + score;
     var testRankingMsg = string.Empty;

     if (score >= 277)
     {
        testRankingMsg = string.Format("Congratulations. Your company receives an A. This means you are ranked in the top {0:P1} of our participants and clients.", rank);
     }
     else if (score >= 246 && score <= 276)
     {
        testRankingMsg = string.Format("Your company receives a B. This means you are ranked in the top {0:P1} of our participants and clients.", rank);
     }
     else if (score >= 215 && score <= 245)
     {
        testRankingMsg = string.Format("Your company receives a C. This means you are ranked in the middle {0:P1} of our participants and clients.", rank);
     }
     else if (score >= 186 && score <= 214)
     {
        testRankingMsg = string.Format("Your company receives a D. This means you are ranked in the bottom {0:P1} of our participants and clients.", rank);
     }
     else
     {
        testRankingMsg = string.Format("Your company receives an F. This means you are ranked in the bottom {0:P1} of our participants and clients.", rank);
     }

     lblScore.Text = testScoreMsg;
     lblRank.Text = testRankingMsg;
  }
4

0 に答える 0