/// <summary>Save the test results to TestResults table</summary>
        /// <param name="ctx">ODataValidationSuiteEntities</param>
        private void ProcessResultsBatch(ODataValidationSuiteEntities ctx)
        {
            // write back to the DB in blocks of 5 test results
            foreach (TestResult res in this.resultsToSave)
            {
                ctx.AddToTestResults(res);
            }

            ctx.SaveChanges();

            Dictionary<string, int> dic = new Dictionary<string, int>();

            // Get the rule name and test result id.
            foreach (TestResult res in this.resultsToSave)
            {
                var resultID = from j in ctx.TestResults
                               where j.RuleName == res.RuleName && j.ValidationJobID == res.ValidationJobID
                               orderby j.ID
                               select j.ID;

                if (resultID.Any())
                {
                    dic.Add(res.RuleName, Int32.Parse(resultID.First().ToString()));
                }
            }

            // Write details information to the DB.
            foreach (ExtensionRuleResultDetail detail in this.details)
            {
                AddTestResultDetailToDB(ctx, dic[detail.RuleName], detail);
            }

            ctx.SaveChanges();
            this.resultsToSave.Clear();
            this.details.Clear();
            dic.Clear();
        }
        private void ProcessResultsBatchByJobCompleted(ODataValidationSuiteEntities ctx, bool isToCompleteJob = false)
        {
            if (this.jobType != JobType.UriRerun
                  && this.jobType != JobType.PayloadRerun
                  && this.jobType != JobType.ConformanceRerun)
            {
                this.ProcessResultsBatch(ctx);
                // Update conformance dependency rules result
                if (this.jobType == JobType.Conformance && isToCompleteJob)
                {
                    ConformanceLevelValidation.UpdateAllConformanceLevelRules(this.jobId);
                }
            }

            if (this.jobType == JobType.UriRerun
                || this.jobType == JobType.PayloadRerun
                || this.jobType == JobType.ConformanceRerun)
            {
                List<TestResult> newTestResult = null;
                UpdateSimpleRerunTestResult(out newTestResult);
                // Update conformance result details and dependency rules result
                if (this.jobType == JobType.ConformanceRerun)
                {
                    UpdateResultDetails(newTestResult);
                    if (isToCompleteJob)
                    {
                        ConformanceLevelValidation.UpdateAllConformanceLevelRules(this.jobId);
                    }
                }
            }
        }
        private void AddTestResultDetailToDB(ODataValidationSuiteEntities ctx, int testResulID, ExtensionRuleResultDetail detail)
        {
            // Write one detail information to the DB.
            ResultDetail resultDetailInDB = new ResultDetail();
            int resultID = testResulID;
            resultDetailInDB.TestResultID = resultID;
            resultDetailInDB.RuleName = detail.RuleName;
            resultDetailInDB.URI = detail.URI;
            resultDetailInDB.HTTPMethod = detail.HTTPMethod;
            resultDetailInDB.RequestHeaders = detail.RequestHeaders;
            resultDetailInDB.RequestData = detail.RequestData;
            if (string.IsNullOrEmpty(detail.ResponseStatusCode))
            {
                resultDetailInDB.ResponseStatusCode = detail.ResponseStatusCode;
            }
            else
            {
                HttpStatusCode? responseStatusCode = (HttpStatusCode)Enum.Parse(typeof(HttpStatusCode), detail.ResponseStatusCode);
                int statusCode = Convert.ToInt32(responseStatusCode);
                resultDetailInDB.ResponseStatusCode = string.Format("{0},{1}", statusCode.ToString(), detail.ResponseStatusCode.ToString());
            }

            resultDetailInDB.ResponseHeaders = detail.ResponseHeaders;
            resultDetailInDB.ResponsePayload = detail.ResponsePayload;
            resultDetailInDB.ErrorMessage = detail.ErrorMessage;

            ctx.AddToResultDetails(resultDetailInDB);
        }