So I have the following objects
public class Person : IValidatableObject
{
[Required]
public string Name {get;set;}
[Required]
public string Status {get;set;
public Address Address {get;set;}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
if (Status != "Addressless" && Address == null)
{
yield return new ValidationResult("An address is required if this person is not addressless");
}
}
}
public class Address
{
[Required]
public string Address1 {get;set;}
[Required]
public string City {get;set;}
}
Now, my problem is that Address values are required because if the person does have an address, EF Code First needs not null values in the DB in the Address table. This seems to be causing an issue because a Person does not need an address, so if they are "Addressless", there should be no Address
validation, but validation still kicks off because Address has required fields.
Is there a way around this?