Search code examples
entity-framework-6data-annotations

Entity Framework - validate text format


I use Code First approach and want to use DataAnnotation (or any other way, i.e. Fluent API) to specify allowed format of text. I.e. I want to use RegularExpressions to validate it etc. I want to do it on Data layer (not on client), I want to see constraints in SQL.

Is it possible?


Solution

  • If you are asking if you can see the regular expression constraint show up on the SQL Server side, the answer is no, SQL Server doesn't support Regex. If you want the EF engine to run validation for a DataAnnotation for a regular expression before it hits the database, then yes you can.

    [System.ComponentModel.DataAnnotations.RegularExpression]