Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EF Code First: IValidatable Object not validating

I have an object in a simple test scenario that uses EF Code First and implements IValidatableObject. There's some very simple logic that adds a validation error and returns it back. There are also other validations on the object.

However, when saving the object - while the attribute based validations work - the IValidatableObject interface never seems to fire. Debugger doesn't step into it and the error never shows up with calling SaveChanges() or GetValidationErrors().

public class Customer : IValidatableObject {
[Key]
public int Id { get; set; }

[StringLength(50)]
[DisplayName("First Name")]

public string FirstName { get; set; }

[Required]
[DisplayName("Last Name")]
[StringLength(50)]
public string LastName { get; set; }

[Required]
[StringLength(100)]
public string Company { get; set; }

[StringLength(200)]
public string Email { get; set; }

[DisplayName("Credit Limit")]
public decimal CreditLimit { get; set; }

[DisplayName("Entered On")]
public DateTime? Entered { get; set; }


public virtual ICollection<Address> Addresses { get; set;  }


public Customer()
{
    Entered = DateTime.Now;
    CreditLimit = 1000.00M;

    Addresses = new List<Address>();
}

public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
    var results = new List<ValidationResult>();

    // add an error ALWAYS for testing - it doesn't show up
    // and the debugger never hits this code
    results.Add(new ValidationResult("Validate Added message",new [] { "Company" }));

    return results;
}

When I now try to add a customer and check for validation errors:

public void AddNewCustomer()
{
    Customer customer = new Customer();  

    context.Customers.Add(customer);

    customer.LastName = "Strahl";
    customer.FirstName = "Rick";
    customer.Entered = DateTime.Now;
    //customer.Company = "West Wind"; // missing causes val error

    var errorEntries = context.GetValidationErrors();
}

I get ONE validation error for the company, but nothing from the IValidatableObject which should ALWAYS fail.

Any idea why?

like image 914
Rick Strahl Avatar asked Feb 27 '11 09:02

Rick Strahl


1 Answers

Quote from Jeff Handley's Blog Post on Validation Objects and Properties with Validator:

When validating an object, the following process is applied in Validator.ValidateObject:

  1. Validate property-level attributes
  2. If any validators are invalid, abort validation returning the failure(s)
  3. Validate the object-level attributes
  4. If any validators are invalid, abort validation returning the failure(s)
  5. If on the desktop framework and the object implements IValidatableObject, then call its Validate method and return any failure(s)

This indicates that what you are attempting to do won't work out-of-the-box because the validation will abort at step #2.

like image 76
Jakub Konecki Avatar answered Nov 07 '22 22:11

Jakub Konecki