2012-03-02 103 views
0

我有3個連接表; ValidationRun有很多結果其中有許多錯誤實體框架新手 - 保存到DB

下面的代碼成功地保存到結果和錯誤表,但不是ValidationRun。

你能看出問題嗎?

private void WriteResultsToDB(SqlDataReader dr, XMLValidator validator)  
     {  
      using (var context = new ValidationResultsEntities())  
      {  
      var run = new ValidationRun { DateTime = DateTime.Now, XSDPath = this.txtXsd.Text };  

      //loop through table containing the processed XML  
      while (dr.Read())  
      {  
       var result = new Result  
       {  
        AddedDateTime = (DateTime)dr["Added"],  
        CustomerAcc = (string)dr["CustomerAcc"],  
        CustomerRef = (string)dr["CustomerRef"]  
       };  

       if (this.rdoRequest.Checked)  
       {  
        result.XMLMsg = (string)dr["RequestMSG"];  
       }  
       else  
       {  
        result.XMLMsg = (string)dr["ReplyMSG"];  
       }  

       if (validator.Validate(result.XMLMsg))  
       {  
        foreach (string error in validator.Errors)  
        {  
         result.Errors.Add(new Error { ErrorDescription = error });  
        }  
       }  
       else  
       {  
        //validator caught an error  
        result.Errors.Add(new Error { ErrorDescription = "XML could not be parsed" });  
       }  

       if (result.Errors.Count == 0) result.ValidFile = true; else result.ValidFile = false;  

       context.AddToResults(result);  
       context.SaveChanges();  
      }  
     } 

回答

1

您似乎沒有將運行添加到上下文的任何部分。如果它被你添加的結果所引用,也許更改跟蹤器會知道它應該被保存,但是因爲它被寫入,所以它只是一些孤立的對象,不會被連接到任何地方。

+0

是的,這就是謝謝。我通過添加「run.Results.Add(result);」 – 2012-03-02 16:14:23