title: How to find out which column(s) violated which constraints (and checks) when calling SaveChangesAsync()
body: <p>Suppose I am building an ASP.NET Core web app using PostgreSQL as datastore (using Npgsql provider) and I have a Person entity model such as</p>
  <pre class="lang-cs prettyprint-override"><code>[Index(nameof(EmailAddress), IsUnique = true)]
  [Index(nameof(EmployeeNo), IsUnique = true)]
  public class Person
  {
      [Key]
      public Guid Id { get; set; }
  
      [Required]
      [MaxLength(20)]
      public string FullName { get; set; }
  
      [Required]
      public string EmailAddress { get; set; }
      
      [Required]
      public int EmployeeNo { get; set; }
  }
  
  </code></pre>
  <p>Now, when user provides me these details by submitting a form and in my business logic I create a Person instance and save to the database using <code>_db.SaveChangesAsync() </code></p>
  <p>Now, suppose there is already a Person in DB with given EmailAddress or EmployeeNo. So <code>_db.SaveChangesAsync() </code> will throw <code>DbUpdateException</code>.</p>
  <p>So, my questions are:</p>
  <ol>
  <li><p>How can I know which of the two - EmailAddress or EmployeeNo - caused this error. Either one of them or both of them might have caused it. So how do I know which one?</p>
  </li>
  <li><p>How to know the cause behind this error: Was it because of the unique index constraint? Or was it because of the <code>[Required]</code> attribute.</p>
  </li>
  <li><p>What kind of error handling techniques (using try-catch) should be used here?</p>
  </li>
  <li><p>What about error happened by integeroverflow, string length mismatch, etc.?</p>
  </li>
  <li><p>How about handling error caused by <code>MaxLength</code> attribute?</p>
  </li>
  </ol>
  <p>Thanks!</p>
  <h4>Include provider and version information</h4>
  <p>EF Core version: 5.0.8<br />
  Database provider: Npgsql.EntityFrameworkCore.Postgres<br />
  Target framework:  .NET 5.0<br />
  Operating system: Windows<br />
  IDE: VS Code</p>