views:

694

answers:

2

I would like to implement some business rule validation like Scott Guthrie did in his MVC Nerddinner tutorial (http://nerddinnerbook.s3.amazonaws.com/Part3.htm), but I'm running into a problem trying to do so.

Scott was using Linq to SQL in his tutorial. He creates partial classes for his data objects, and then he implements a partial method called OnValidate(), which, according to him, is a hook that gets ran when data is persisted to the database for a given data object.

public partial class Dinner {

  partial void OnValidate(ChangeAction action) {

    if (!IsValid)
      throw new ApplicationException("Rule violations prevent saving");
    }

}

My problem is that I am using Linq to Entities and apparently there is no "hook" method like the one above that one can use, or at least I can't find it if there is one. Can anyone throw me a bone on how to go about doing this with Linq to Entities?

A: 

Check:

Robert Koritnik
You're links seem only to apply to Linq to SQL, and not Linq to Entities. Unfortunately, the same concepts that work with Linq to SQL do not work with Linq to Entities when it comes to data validation during a save.
Jagd
No. You're wrong. Data Annotations work with ANY classes. Also EF generated ones. Except that you'll work with MetadataType attribute and classes that will define this metadata type.
Robert Koritnik
+2  A: 

You can do data validation in linq-to-entities, but it is different from linq-to-sql, as you've discovered. See the following link:

data validation in linq-to-entities

The Beaver
Awesome! The link you supplied is exactly what I was looking for. Thanks again.
Jagd