title: Catch and Resolve Entity Validation Errors
body: <p>I'm using code-first EF 4.1 to insert data into a SQL Server database.  When a string property has a value longer than the maximum set in the mapping, EF throws a <code>DbEntityValidationException</code> whose <code>EntityValidationsErrors</code> contains the details of the issue.</p>
  
  <p>Is there any way to resolve the errors programmatically?</p>
  
  <p>Specifically I would like to truncate the property in question, record a "Property X Truncated" notification for later use and re-attempt the <code>SaveChanges()</code>.</p>
  
  <p>I've created a custom <code>ValidationAttribute</code> that checks the length of the annotated property but can't figure out if I can change the property's length at the same time.</p>
  
  <pre><code>public class TruncateAttribute : ValidationAttribute
  {
      public int TruncateLength { get; set; }
  
      public TruncateAttribute(object truncateLength)
      {
          this.TruncateLength = (int) truncateLength;
      }
      protected override ValidationResult IsValid(object value, 
            ValidationContext validationContext)
      {
          var original = (string) value;
          if (original.Length &gt; this.TruncateLength)
          {
              value = original.Substring(0, 
                       this.TruncateLength); // doesn't work
              return new ValidationResult(
                  string.Format("{0} is longer than {1} characters",
                    validationContext.DisplayName, this.TruncateLength),
                  new[] {validationContext.MemberName});
          }
          else
          {
              return ValidationResult.Success;
          }
      }
  }
  </code></pre>