Thursday, July 30, 2009

Validate All SubmitChanges

Source Code

2009-08-24 - I have updated the code to incude an extension method for EntitySet addition called ValidatePriorToAddOnSubmit that will only .Add the entity if it passes. Also I have updated my sample code to show additional flows for validating when attaching to EntitySet or attaching a new entity as the EntityRef.

Unfortunately LINQ to SQL does not provide a way to get an accumulated list of errors after the SubmitChanges event. The default behavior is to not commit the transaction if there are ANY validation exceptions.

I have written a few extension methods to get around this behavior. With these extension methods updates, inserts, deletes, and adds will be cancelled if the entity does not pass OnValidate. This allows you to collect all of the invalid entities and yet still allow others to get processed.





I found this pattern very helpful when writing import routines and also grids that have all rows in edit mode. I'm sure there are many other places where this can be helpful. Here is a snippet using xml to deserialize and then persist only valid entities to the database.



No comments:

Post a Comment