9

I'm doing code-first development with Entity Framework 4.3 and it doesn't seem like it's possible to express a CHECK constraint via attribute annotations or, well, any other means. I see that EF 5.0 will be adding support for checking enumerations, but that's not exactly what I'm after here.

To give a simplified example, I'd like to validate that all Person objects have a first name of either "Bob" or "Harry" and are either 5, 10 or 30 years old.

public class Person
{
    [Required]
    [Check("Bob", "Harry")]  //yes, this attribute is imaginary
    public string FirstName { get; set; }

    [Required, Check(5, 30, 50)]  //check is still imaginary
    public int Age { get; set; }
}

I can run an alter script to add these constraints after the fact and I can roll my own check attribute to perform validations, but is there a way I'm missing to actually express non-enumerated CHECK constraints in Entity Framework?

48klocs
  • 6,073
  • 3
  • 27
  • 34

2 Answers2

6

I've been wanting to put Check constraints in, and while there are a couple of ways to do it like the answer given here by M.Babcock and using the ExecuteSql in the Initializer to add constraints to the database manually.

But I think the easiest method is to use a RegularExpression annotation, so in your example you'd go:

public class Person
{
    [Required]
    [RegularExpression(@"Bob|Harry")]  
    public string FirstName { get; set; }

    [Required, RegularExpression(@"5|30|50")]  
    public int Age { get; set; }
}
today
  • 32,602
  • 8
  • 95
  • 115
sh545
  • 61
  • 2
  • This only works if your check constraint can be validated by a regular expression. This works for the //simplified examples// of the OP. In other case, a custom validation, as described by @M.Babcock will be useful. – R. Schreurs Oct 30 '14 at 13:35
3

You could write one yourself (untested):

public class CheckAttribute : System.ComponentModel.DataAnnotations.ValidationAttribute
{
    object[] ValidValues;

    public CheckAttribute<T>(params T[] validValues)
    {
        ValidValues = validValues;
    }

    public override bool IsValid(object value)
    {
        return ValidValues.FirstOrDefault(v => v.Equals(value)) != null;
    }
}
M.Babcock
  • 18,753
  • 6
  • 54
  • 84
  • I did write my own check attribute (that looks eerily similar to yours), but my issue is that this is a domain-only attribute - it's not reflected in the backing table entity with a check constraint there as well in the same way that, say, a string's MaxLength gets reflected in the table structure. – 48klocs Mar 06 '12 at 18:47
  • 1
    I'm not all that well versed in EF but I opened `EntityFramework.dll` in ILSpy and it looks like `System.Data.Entity.Internal.Validation.EntityValidatorBuilder` is responsible for analyzing the attributes and deciding what to do about them. It looks as though `RequiredAttribute`, `MaxLengthAttribute`, `StringLengthAttribute`, and `DisplayAttribute` are basically hardwired into the database creation process. I'm not sure it is possible to do what you're after with EF as it is today. – M.Babcock Mar 06 '12 at 18:58