Is it possbile to call Validate(..) without using DbContext?
I want to use it in Unit Tests.
If I use TryValidateObject(..) on my Contract object – only the validation of User Property is called, but not Validate(..)
Here is code of my Entity:
[Table("Contract")]
public class Contract : IValidatableObject
{
[Required(ErrorMessage = "UserAccount is required")]
public virtual UserAccount User
{
get;
set;
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
...
}
...
}
Yes,
you need to call Validator.TryValidateObject(SomeObject,…)
here is an Example
http://odetocode.com/blogs/scott/archive/2011/06/29/manual-validation-with-data-annotations.aspx
… the juicy bit is….
Let me explain better, You need to have all Annotations Valid BEFORE the Validator will call the validate routine, here I added a test program to illustrate what is most likely your issue
}