I am trying to validate a class that has a partial class and a metadata class. When I specify an attribute directly in the class, validation works, and when through metadata, then no. How do I validate my class?
public class EntityMetadata
{
[Required] // when through metadata it doesn’t work
public string Property { get; set; }
}
[MetadataType(typeof(EntityMetadata))]
public partial class Entity
{
}
public partial class Entity
{
[Required] // when directly it works
public string Property { get; set; }
}
class Program
{
static void Main(string[] args)
{
Entity entity = new Entity();
var context = new ValidationContext(entity, null, null);
var results = new List<ValidationResult>();
Validator.TryValidateObject(entity, context, results, true);
}
}