Validation Error on SaveChanges()

前端 未结 3 1284
时光说笑
时光说笑 2020-12-23 02:35

I have the following Action method inside my Asp.net mvc web application:-

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(SDJoin sdj, FormC         


        
相关标签:
3条回答
  • 2020-12-23 02:46

    You haven't shown the Save() method but if you can add code like this to it you'll get an exception that contains all the details you're looking for

    try
    {
        _context.SaveChanges();
    }
    catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
    {
        Exception raise = dbEx;
        foreach (var validationErrors in dbEx.EntityValidationErrors)
        {
            foreach (var validationError in validationErrors.ValidationErrors)
            {
                string message = string.Format("{0}:{1}", 
                    validationErrors.Entry.Entity.ToString(),
                    validationError.ErrorMessage);
                // raise a new exception nesting
                // the current instance as InnerException
                raise = new InvalidOperationException(message, raise);
            }
        }
        throw raise;
    }
    
    0 讨论(0)
  • 2020-12-23 02:46

    Although this is an old post but this approach can be more fruitful!

    Credits

    Try something like this

            try
            {
                pcontext.SaveChanges();
            }
           catch (System.Data.Entity.Infrastructure.DbUpdateConcurrencyException ex)
            {             
                 Console.WriteLine(ex.InnerException);
            }
            catch (System.Data.Entity.Core.EntityCommandCompilationException ex)
            {
              Console.WriteLine(ex.InnerException);
            }
            catch (System.Data.Entity.Core.UpdateException ex)
            {
             Console.WriteLine(ex.InnerException);
            }
    
            catch (System.Data.Entity.Infrastructure.DbUpdateException ex) //DbContext
            {
                Console.WriteLine(ex.InnerException);
            }
    
            catch (Exception ex)
            {
                Console.WriteLine(ex.InnerException);
                throw;
            }
    
    0 讨论(0)
  • 2020-12-23 02:49

    I know the question is old now, but I hope someone finds this useful, too.

    1. Most likely (and indeed, judging by your comments) the error was that your variable doesn't pass Model validation.
    2. The other way to see the errors you couldn't see (without changing the code) is to look at the exception using the QuickWatch window. The problem is that 'View Details' window that you can open from exception tooltip doesn't show EntityValidationErrors. And without a catch block you don't have any exception variable you can look at in QuickWatch. Luckily, QuickWatch has PseudoVariables. So, using "$exception" pseudovariable you can easily take a look at the current unhandled exception in QuickWatch window. (Please, see screenshots below) Exception Details window and QuickWatch window with $exception pseudovariable
    0 讨论(0)
提交回复
热议问题