Validation block in Linq to SQL

Topics: Validation Application Block
Jun 12, 2014 at 9:20 PM
I have a Linq to SQL project. I create a DBML file and drag some tables to it.

The objects are now avaiable (i.e. the name of the table) without me creating any classes. So how could i tie in the Validation Block or is this not possible?

Jun 16, 2014 at 7:40 AM
You could use a configuration file to create validations for the generated LINQ To SQL entities. Or you could create a partial classes for the generated LINQ To SQL entities and use the MetadataType along with a buddy class with the actual validation attributes. Something like this:
public partial class Product
    public class ProductMetaData

        [Range(10, 1000, 
            ErrorMessage = "Value for {0} must be between {1} and {2}.")]
        public object Weight;

        [Range(300, 3000)]
        public object ListPrice;

        [Range(typeof(DateTime), "1/2/2004", "3/4/2004",
            ErrorMessage = "Value for {0} must be between {1} and {2}")]
        public object SellEndDate;
The above sample is using DataAnnotations.
Speaking of DataAnnotations, I would take a look at the out of the box DataAnnotations and see if those work for you since most of the functionality in the Validation Application Block can be had using DataAnnotations.

Randy Levy
Enterprise Library support engineer
Support How-to