我有一个问题,我似乎无法用 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;
}