Checking restrictions on insert or submit of linq-to-sql items to db

364 views Asked by At

I would like to implement checking of object data when inserting or submitting the object to sql. Is there some OnInsert or OnSubmit function that I could implement/extend for the automatically generated linq-to-sql-classes, or some other solution?

The normal way to do this would be to put restrictions, check-constraints, triggers and stored procedures in the sql server db, but I would like to avoid that in order to keep my code in one place and get syntax checking etc.

(I think checking of unique indexes and foreign keys are better implemented directly in the sql db)

1

There are 1 answers

0
Pleun On BEST ANSWER

You can use the OnValidate()function with the LINQ-to-SQL Entity's partial class. OnValidate() will then be called during SubmitChanges() but before the data is sent to the database.

More info on... stackoverflow.... Validate Linq2Sql before SubmitChanges()