예제 #1
0
        /*
         * Catch DbEntityValidationException when db.SaveChanges() is called
         */
        public static void DemoUpdate()
        {
            try
            {
                using (var db = new AppDbTemplateEntities())
                {
                    db.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.
                throw new DbEntityValidationException(exceptionMessage, ex.EntityValidationErrors);
            }
        }
예제 #2
0
 public static List <EmployeeInformation> DbTest()
 {
     using (var db = new AppDbTemplateEntities())
     {
         return(db.EmployeeInformations
                .Where(x => x.CurrentStatusCode.ToLower() != "t")
                .OrderBy(x => x.LastHireDate)
                .Take(10)
                .ToList());
     }
 }