Data validation is a critical component of any data-centric application, so it may come as a surprise to discover that Entity Framework doesn’t provide a validation API. Instead, it depends on standard .NET techniques. If you’re using the ObjectContext
API, that means handlng property changing events or extending the SaveChanges()
method (or adding your own validation layer, which is pretty advanced stuff). The DbContext
API, however, adds support for the IValidatableObject
interface, which not only provides a powerful mechanism for validating entities, but integrates with other .NET technologies and tools. Here’s how DbContext
validation works:
This is called the VALIDATION PIPELINE. All of the methods are public, and you can call them from your own code wherever you need to.