public static void SaveCodeReviewToDatabase(SqlSyncBuildData.CodeReviewRow codeReviewRow)
        {
            var task = Task.Factory.StartNew(() =>
            {
                try
                {
                    CodeReview r = new CodeReview()
                    {
                        CheckSum      = codeReviewRow.CheckSum,
                        CodeReviewId  = codeReviewRow.CodeReviewId,
                        Comment       = codeReviewRow.Comment,
                        ReviewBy      = codeReviewRow.ReviewBy,
                        ReviewDate    = codeReviewRow.ReviewDate,
                        ReviewNumber  = codeReviewRow.ReviewNumber,
                        ReviewStatus  = codeReviewRow.ReviewStatus,
                        ScriptId      = codeReviewRow.ScriptId,
                        ValidationKey = codeReviewRow.ValidationKey
                    };


                    SqlCodeReviewEntities e = GetNewEntity();
                    e.AddToCodeReviews(r);
                    e.SaveChanges();
                    log.DebugFormat("Saved new CodeReview entity for {0}.", codeReviewRow.CodeReviewId);
                }
                catch (Exception exe)
                {
                    log.Error(String.Format("Unable to save new CodeReview entity for {0}.", codeReviewRow.CodeReviewId), exe);
                }
            });
        }
        /// <summary>
        /// Create a new CodeReview object.
        /// </summary>
        /// <param name="codeReviewId">Initial value of the CodeReviewId property.</param>
        /// <param name="reviewStatus">Initial value of the ReviewStatus property.</param>
        public static CodeReview CreateCodeReview(global::System.Guid codeReviewId, global::System.Int32 reviewStatus)
        {
            CodeReview codeReview = new CodeReview();

            codeReview.CodeReviewId = codeReviewId;
            codeReview.ReviewStatus = reviewStatus;
            return(codeReview);
        }
        public static void UpdateCodeReviewToDatabase(SqlSyncBuildData.CodeReviewRow codeReviewRow)
        {
            var task = Task.Factory.StartNew(() =>
            {
                try
                {
                    SqlCodeReviewEntities e = GetNewEntity();
                    var r = from cr in e.CodeReviews
                            where cr.CodeReviewId == codeReviewRow.CodeReviewId
                            select cr;
                    if (!r.Any())
                    {
                        SaveCodeReviewToDatabase(codeReviewRow);
                    }
                    else
                    {
                        CodeReview review    = r.First();
                        review.CheckSum      = codeReviewRow.CheckSum;
                        review.Comment       = codeReviewRow.Comment;
                        review.ReviewBy      = codeReviewRow.ReviewBy;
                        review.ReviewDate    = codeReviewRow.ReviewDate;
                        review.ReviewNumber  = codeReviewRow.ReviewNumber;
                        review.ReviewStatus  = codeReviewRow.ReviewStatus;
                        review.ScriptId      = codeReviewRow.ScriptId;
                        review.ValidationKey = codeReviewRow.ValidationKey;

                        e.SaveChanges();
                    }
                    log.DebugFormat("Updated CodeReview entity for {0}.", codeReviewRow.CodeReviewId);
                }
                catch (Exception exe)
                {
                    log.Error(String.Format("Unable to update CodeReview entity for {0}.", codeReviewRow.CodeReviewId), exe);
                }
            });
        }
 internal static void SyncXmlReviewDatatoEfReview(SqlSyncBuildData.CodeReviewRow row, CodeReview review)
 {
     //var rows = codeReviewTable.Where(t => t.CodeReviewId == codeReviewId);
     //if (rows.Count() > 0)
     //{
     //    SqlSyncBuildData.CodeReviewRow row = rows.First();
     review.CheckSum      = row.CheckSum;
     review.CodeReviewId  = row.CodeReviewId;
     review.Comment       = row.Comment;
     review.ReviewBy      = row.ReviewBy;
     review.ReviewDate    = row.ReviewDate;
     review.ReviewNumber  = row.ReviewNumber;
     review.ReviewStatus  = row.ReviewStatus;
     review.ScriptId      = row.ScriptId;
     review.ValidationKey = row.ValidationKey;
 }
        public static SqlSyncBuildData LoadCodeReviewData(SqlSyncBuildData buildData, out bool databaseSuccess)
        {
            //Make sure the table it empty...
            SqlSyncBuildData.CodeReviewDataTable crTable = buildData.CodeReview;

            //Get the list of script ids...
            var scriptIds = from s in buildData.Script
                            select s.ScriptId;
            List <string> lstScriptIds = scriptIds.ToList();

            try //sync the database and the local reviews if possible...
            {
                SqlCodeReviewEntities e = GetNewEntity();

                //Get all reviews from the database that match our scripts...
                var reviews = from crEntity in e.CodeReviews
                              where lstScriptIds.Contains(crEntity.ScriptId)
                              select crEntity;
                //string sql = ((System.Data.Objects.ObjectQuery)reviews).ToTraceString();
                List <CodeReview> lstDatabaseReviews = reviews.ToList();


                //find the matching EF CodeReview and CodeReview table entries and sync as appropriate
                var matches = from r in lstDatabaseReviews
                              join c in crTable on r.CodeReviewId equals c.CodeReviewId
                              select new { r, c };
                var matchList = matches.ToList();

                //Update the EF reviews where there is a matching, more current local version
                bool localChanges = false;
                foreach (var pair in matchList)
                {
                    //Only need to update if the XML review is more current...
                    if (pair.c.ReviewDate > pair.r.ReviewDate)
                    {
                        SyncXmlReviewDatatoEfReview(pair.c, pair.r);
                        localChanges = true;
                    }
                }


                //Add to the EF reviews if there are local ones that don't exist in the database...
                var         efReview    = from r in reviews select r.CodeReviewId;
                List <Guid> efReviewIds = efReview.ToList();
                var         localOnlyId = (from c in crTable
                                           select c.CodeReviewId).Except(efReviewIds);

                var localOnlyRow = from c in crTable
                                   where localOnlyId.Contains(c.CodeReviewId)
                                   select c;

                foreach (SqlSyncBuildData.CodeReviewRow row in localOnlyRow)
                {
                    CodeReview codeReview = new CodeReview();
                    SyncXmlReviewDatatoEfReview(row, codeReview);
                    lstDatabaseReviews.Add(codeReview);  //add to local collection
                    e.CodeReviews.AddObject(codeReview); //add to database collection
                    localChanges = true;
                }

                if (localChanges)
                {
                    e.SaveChanges();
                }


                //Now that the database can be considered the "master" set of data, load it up!
                try
                {
                    //Clear out the local rows and pull from database...
                    buildData.CodeReview.Rows.Clear();

                    List <SqlSyncBuildData.CodeReviewRow> newRows = new List <SqlSyncBuildData.CodeReviewRow>();
                    Parallel.ForEach(buildData.Script, row =>
                    {
                        string scriptId = row.ScriptId;

                        var match = from r in lstDatabaseReviews
                                    where r.ScriptId == scriptId
                                    select r;

                        foreach (CodeReview cr in match)
                        {
                            SqlSyncBuildData.CodeReviewRow newRow = buildData.CodeReview.NewCodeReviewRow();
                            newRow.ScriptId      = row.ScriptId;
                            newRow.CheckSum      = cr.CheckSum;
                            newRow.CodeReviewId  = cr.CodeReviewId;
                            newRow.Comment       = cr.Comment;
                            newRow.ReviewBy      = cr.ReviewBy;
                            newRow.ReviewDate    = (DateTime)cr.ReviewDate;
                            newRow.ReviewNumber  = cr.ReviewNumber;
                            newRow.ReviewStatus  = (short)cr.ReviewStatus;
                            newRow.ValidationKey = cr.ValidationKey;
                            newRow.ScriptRow     = row;

                            newRows.Add(newRow);
                        }
                    });

                    //Add them on the same thread...
                    foreach (SqlSyncBuildData.CodeReviewRow newRow in newRows)
                    {
                        buildData.CodeReview.AddCodeReviewRow(newRow);
                    }

                    buildData.AcceptChanges();
                    databaseSuccess = true;
                }
                catch (System.Data.Entity.Core.EntityException exe)
                {
                    log.Error(
                        "Unable to connect to SqlCodeReview database. Code Reviews will not be sync'd with the database",
                        exe);
                    databaseSuccess = false;
                }
                catch (Exception generalExe)
                {
                    log.Error("Unable to read SqlCodeReview", generalExe);
                    databaseSuccess = false;
                }
            }
            catch (System.Data.SqlClient.SqlException sqlExe)
            {
                log.Error(string.Format("Current user '{0}' errored at the database", System.Environment.UserName), sqlExe);
                databaseSuccess = false;
            }
            catch (Exception ex)
            {
                log.Error(
                    "Unable to sync local code reviews with SqlCodeReview database. Code Reviews will not be sync'd with the database",
                    ex);
                databaseSuccess = false;
            }
            return(buildData);
        }
 /// <summary>
 /// Deprecated Method for adding a new object to the CodeReviews EntitySet. Consider using the .Add method of the associated ObjectSet&lt;T&gt; property instead.
 /// </summary>
 public void AddToCodeReviews(CodeReview codeReview)
 {
     base.AddObject("CodeReviews", codeReview);
 }