I want to validate each entity when saving. That is, I have some custom C# function in each entity class that validates the data.
How can I do that? I don’t want to use database constraints because they cannot express my constraints.
Should I implement some interface???
Entity framework 3.5 and 4.0 offers even called
SavingChanges. Entity framework 4.0 and 4.1 hasSaveChangesmethod virtual (as already mentioned).You can either override method or use event handler and write code like this for 3.5 and 4.0:
In DbContext API (EF 4.1) you must use
You can use custom interface implemented by your entities which will expose
Validatemethod.