0

如何在这个 linq to sql 代码中添加事务

MyDBDataContext DB = new MyDBDataContext();
            SurveyClient objMaster = new SurveyClient();
            objMaster.SurveyID = int.Parse(dtQuestions.Rows[0]["SurveyID"].ToString());
            MembershipUser myObject = Membership.GetUser();
            myObject.ProviderUserKey.ToString();
            objMaster.UserID = Guid.Parse(myObject.ProviderUserKey.ToString());  //Guid.Parse("993a109d-a0c7-4946-a8da-99fb594f3ce2");// current userID
            objMaster.SurveyDate = DateTime.Now;
            DB.SurveyClients.InsertOnSubmit(objMaster);
           // DB.SubmitChanges();

            foreach (DataRow dr in dtQuestions.Rows)
            {
                int currQueScore = GetAnswerScore(dr["AnswerType"].ToString().Trim(), dr["ClientAnswerValue"].ToString().Trim());
                dr["ClientAnswerScore"] = currQueScore;
                myScore += currQueScore;
                SurveyClientAnswer objDetail = new SurveyClientAnswer();
                objDetail.SurveyClientID = objMaster.SurveyClientID;
                objDetail.QuestionID = int.Parse(dr["QuestionID"].ToString());
                objDetail.Answer = dr["ClientAnswerValue"].ToString();
                objDetail.Score = int.Parse(dr["ClientAnswerScore"].ToString());
                DB.SurveyClientAnswers.InsertOnSubmit(objDetail);
               // DB.SubmitChanges();
            }
            objMaster.FinalScore = myScore;
            DB.SubmitChanges();

当我评论两个 DB.SubmitChanges() 时,它会抛出错误

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_SurveyClientAnswers_SurveyClientAnswers". The conflict occurred in database "NEXLEV", table "dbo.SurveyClient", column 'SurveyClientID'.
The statement has been terminated.
4

2 回答 2

1

您走在正确的道路上,但是考虑了太多“面向数据库”。

实际上 objMaster 还没有 ID,这就是问题所在。但是在 Linq 中,您不应该将主 ID 分配给细节,而是将主 ID 分配给细节实体类(或相反):

Sohe的问题是这一行:

objDetail.SurveyClientID = objMaster.SurveyClientID; 

它应该改为

objDetail.SurveyClient = objMaster; 

在这种情况下,不需要事务管理,因为它可以在一个 SubmitChanges() 中工作,Linq 会为你添加事务,它也足够聪明,可以以正确的方式管理 ID 的设置

[假设:您在数据库中设置了外键约束]

于 2012-05-30T16:25:08.397 回答
0

您没有分配 objMaster.SurveyClientID,因为它不在数据库中,并且在您将其分配给您的 SurveyClientAnwser 时没有 ID。第一个(注释的) DB.SubmitChanges() 确实将更改发送到数据库,并且 objMaster.SurveyClientID 将被分配(在您的数据库中自动分配)。然后 objDetail.SurveyClientID = objMaster.SurveyClientID; 行确实为您的 SurveyClientAnwser 分配了一个真实的 ID。

要进行一项交易,您必须制定一个交易范围:

  MyDBDataContext DB = new MyDBDataContext();
  using (TransactionScope ts = new TransactionScope())
  {
      SurveyClient objMaster = new SurveyClient();
      objMaster.SurveyID = int.Parse(dtQuestions.Rows[0]["SurveyID"].ToString());
      MembershipUser myObject = Membership.GetUser();
      myObject.ProviderUserKey.ToString();
      objMaster.UserID = Guid.Parse(myObject.ProviderUserKey.ToString());  //Guid.Parse("993a109d-a0c7-4946-a8da-99fb594f3ce2");// current userID                  
      objMaster.SurveyDate = DateTime.Now;                  DB.SurveyClients.InsertOnSubmit(objMaster);
      DB.SubmitChanges();
      foreach (DataRow dr in dtQuestions.Rows)                 
      {
            int currQueScore = GetAnswerScore(dr["AnswerType"].ToString().Trim(), dr["ClientAnswerValue"].ToString().Trim()); 
            dr["ClientAnswerScore"] = currQueScore;
            myScore += currQueScore;  
            SurveyClientAnswer objDetail = new SurveyClientAnswer();                      
            objDetail.SurveyClientID = objMaster.SurveyClientID;   
            objDetail.QuestionID = int.Parse(dr["QuestionID"].ToString()); 
            objDetail.Answer = dr["ClientAnswerValue"].ToString(); 
            objDetail.Score = int.Parse(dr["ClientAnswerScore"].ToString());       
            DB.SurveyClientAnswers.InsertOnSubmit(objDetail);
            // DB.SubmitChanges(); //no need for this one
      }
      objMaster.FinalScore = myScore;
      DB.SubmitChanges();      

      ts.Complete();
  }
于 2012-05-30T11:35:06.723 回答