コード例 #1
0
        //[HttpPost]
        public void InsertProjects(IEnumerable <Project> projects)
        {
            try
            {
                using (Estimator dbContext = new Estimator())
                {
                    foreach (Project proj in projects)
                    {
                        if (proj.Id != 0)
                        {
                            var existingProject = dbContext.Projects.Find(proj.Id);

                            DbEntityEntry <Project> ee = dbContext.Entry(existingProject);
                            ee.CurrentValues.SetValues(proj);
                        }
                        else
                        {
                            //dbContext.Projects.AddRange(projects);
                            dbContext.Projects.Add(proj);
                        }
                        dbContext.SaveChanges();
                    }
                }
            }
            catch (DbEntityValidationException ex)
            {
                // Retrieve the error messages as a list of strings.
                var errorMessages = ex.EntityValidationErrors
                                    .SelectMany(x => x.ValidationErrors)
                                    .Select(x => x.ErrorMessage);

                // Join the list to a single string.
                var fullErrorMessage = string.Join(";", errorMessages);

                // Combine the original exception message with the new one.
                var exceptionMessage = string.Concat(ex.Message, " The validation errors are", fullErrorMessage);

                // Throw a new DbEntityValidationException with the improved exception message.
            }
        }
コード例 #2
0
        public int SaveEstimateVersion(EstimationVersionsHistory estimateversion)
        {
            int versionHistoryID = -1;
            EstimationOperations estOperation = new EstimationOperations();

            using (Estimator dbContext = new Estimator())
            {
                if (estimateversion.Id != 0)
                {
                    var existingversionHistory = dbContext.VersionHistory.Find(estimateversion.Id);
                    DbEntityEntry <EstimationVersionsHistory> ee = dbContext.Entry(existingversionHistory);
                    ee.CurrentValues.SetValues(estimateversion);
                }
                else
                {
                    //dbContext.Projects.AddRange(projects);
                    dbContext.VersionHistory.Add(estimateversion);
                }
                dbContext.SaveChanges();
                versionHistoryID = estimateversion.Id;
            }

            return(versionHistoryID);//estOperation.GetCategories();
        }