Question

How can I specify a Check Constraint in EF4 CodeFirst.

Example: I have a string property for which I can have only specific values.

Was it helpful?

Solution

EF code first doesn't have any equivalent for check constraints. If you want to have check constraint in the database you can either use custom database initializer and execute custom SQL to alter table and add check constraint (I described similar approach for unique index here) or if you are already using EF code first migrations you can add constraint creation to your Up method in code based migration.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top