Adding Validation to the Data Model

Introduction

ASP.NET Dynamic Data uses a data model to interact with the underlying database. In the model, a set of classes represents the database entities. You can generate the classes through the Object Relational Designer (O/R Designer) that comes with Visual Studio.

Note

The O/R Designer is a visual design surface for creating LINQ to SQL entity classes and associations based on objects in a database. Dynamic Data queries the database using LINQ and uses the model to perform update, insert, and delete operations. This provides an easy way to integrate data validation and business logic rules into your data model. For more information about the O/R Designer, see Object Relational Designer (O/R Designer).

During insert and edit operations, Dynamic Data provides built-in data validation based on the database schema that was used to create the data model. The following list shows the validation types performed automatically by Dynamic Data:

  • Required-field validation. If a database field cannot be null, Dynamic Data enforces that a value is provided for the field.

  • Length validation. If a database field is a string, Dynamic Data enforces the maximum length of the field.

  • Type validation. If a database field is a DateTime, decimal, double, or integer type, Dynamic Data enforces that the value that is provided matches the database field type.

ASP.NET Dynamic Data provides a flexible model and enables you to customize and extend the built-in data validation. The following are some of the possible customizations:

  • Adding validation to the data model by using attributes. For example, you can add a RangeAttribute attribute to an entity class to specify a valid numeric interval for a specific field.

  • Adding validation to the data model by using event handlers that are called when a field is changed. For example, the data model raises OnFieldChanging events for each table field. If your database contains a Product table with a column named ProductName, the entity class contains a method named OnProductNameChanging that you can override to perform data validation.

In This Section

How to: Add Validation to the Data Model Using Attributes

Demonstrates how to perform data validation by adding attributes in the data model.

How To: Add Validation to the Data Model Using DynamicValidator

Demonstrates how to perform custom data validation by overriding the OnFieldChanging event on the data model.

This topic is ASP.NET Extensions documentation and is unsupported by Microsoft. Blank topics are included as placeholders and existing content is subject to change in future releases.