I am using MVC4, EF5 and SQL Server 2012 Express LocalDB. The Customer
domain model has Phone
and Email
fields. Either can be null, but, at least one must be populated.
I make sure of this using FluentValidation on both the client and server sides.
But, when I need to persist the model, I run into a limitation with EF5. If I make both required, then the persist code fails, but if I make them both optional:
...
this.Property(q => q.Phone).IsOptional();
this.Property(q => q.Email).IsOptional();
then this doesn't meet my requirements.
Q1: How do I model this - is there a simple way, or must I use a check constraint? Is this check constraint okay:
ALTER TABLE dbo.[Customer]
ADD CONSTRAINT constraintAtLeastOneContactDetailRequired
CHECK ( ([Phone] IS NOT NULL) OR ([Email] IS NOT NULL) )
--CHECK ( NOT(COALESCE([Phone], [Email]) IS NULL) ) <--or this?
;
Q2: I have a UNIQUE constraint on both fields, so if I allow them to be null and add the check constraint, will I have problems since there can be multiple nulls which are by definition not unique?