ASP.NET Dynamic Data Model Overview
Updated: August 2008
The data model represents the information that is in a database and how the items in the database are related to each other. ASP.NET Dynamic Data uses data models that represent data as CLR types. It supports LINQ-to-SQL and ADO.NET Entity Framework data models.
Through the data model you can control the appearance and behavior of data fields and perform automatic validation with little or no code. In addition, you can customize the data model to affect how data fields are validated and how the UI for data field display and editing is rendered.
By understanding and working with the data model, you can do the following:
Make your code more reusable by creating data-related logic in the data layer instead of embedding it in the presentation layer.
Customize data field validation in the data layer.
Customize the appearance and behavior of data fields by applying metadata to them.
This topic contains the following sections:
Dynamic Data supports the LINQ to SQL and the Entity Framework data models, which are both included in the NET Framework. These models contain CLR types that Dynamic Data uses to query the database and to perform create, read, update and delete (CRUD) operations. The models provide an easy way to integrate data validation and business logic rules into the data model.
When you create a new project in Visual Studio 2008, you can select the LINQ to SQL Classes or the ADO.NET Entity Data Model template. This selection determines the type of model that is used by the project—that is, whether to use a LINQ-to-SQL or an Entity Framework model. Dynamic Data scaffolding can support only one of the data model types in the same project.
At run time, Dynamic Data automatically extracts information about the data model, such as data field properties. From this information, it infers how to create UI for displaying and editing data. To render the UI, it uses field templates. For example, Dynamic Data uses the following information to render UI:
Information about associations between tables is used to display foreign-key columns and navigate between tables.
Data-type information is used to add validation for a data field. For example, null column information can be used to determine whether a data field is required, and length information can be used to restrict the maximum length of user's text input.
For more information, see How to: Customize Data Field Appearance and Behavior in the Data Model.
Displaying and Modifying Data Fields
Dynamic Data uses the data-model metadata to automatically determine which field templates to use to render the UI for displaying and modifying data fields. Field templates are user controls that Dynamic Data provides to display and modify the data fields based on their intrinsic data types. To render a data type, Dynamic Data performs a search through the data model, looking for classes that have the UIHintAttribute attribute applied.
If the attribute exists, it specifies which field template to use to display and modify a particular data-field type. If no attribute exists, Dynamic Data searches for a field template whose name matches the data field type. For example, to display an integer value, Dynamic Data searches for a field template named Integer.ascx. For more information, see ASP.NET Dynamic Data Field Templates Overview.
Dynamic Data lets you customize and extend the UI that is rendered for data field display and editing. You can apply the following customizations:
Add custom metadata to data fields. You can use attributes to assign metadata to data fields to customize how the UI for that data field is rendered for display and editing. For example, you can use the UIHintAttribute attribute to associate a custom field template with a data field. For more information, see How to: Customize Data Field Appearance and Behavior in the Data Model.
Add non-intrinsic type information to data-field types. You can use attributes to assign a type to a data field that is not derived directly from the data source. For example, you can use the DataTypeAttribute attribute to associate an additional, non-intrinsic type with a data field. For more information, see Dynamic Data Attributes later in this topic and How to: Customize Data Field Appearance and Behavior For Non-Intrinsic Data Types in the Data Model.
Dynamic Data Attributes
Attributes are descriptive declarations that are used to annotate programming elements such as types, fields, methods, and properties. You can provide additional information to Dynamic Data by using attributes to apply metadata to data fields in a data model. For example, you can use the metadata information to customize the way that the UI for a data field is rendered for display and editing.
The following table lists the attributes that are available in Dynamic Data.
Adds information about data fields that are typically treated as strings. For information about data types that are supported by Dynamic Data, see the DataType class overview.
Changes the default parent table column that Dynamic Data uses for a foreign-key field.
This attribute can be applied only to a class, and it can be applied only one time. The attribute can be inherited by derived classes.
Applies formatting to the data in a data field.
Associates a class with a data-model partial class. In the associated class, you can provide metadata that is not defined in the data model.
Provides range-based validation where the value of a field must be between a minimum and maximum value. The built-in field templates for Dynamic Data use the RangeValidator control for range-based validation.
Provides validation based on a regular expression where the value of a field must match a specified pattern. The built-in field templates for Dynamic Data use the RegularExpressionValidator control for regular-expression-based validation.
Specifies that the field is required. The built-in field templates for Dynamic Data use the RequiredFieldValidator control for required fields.
Enables or disables scaffolding of data columns in a data model. This attribute is applied to a data field object in the class that represents the table.
Enables or disables scaffolding of data tables in a data model. This attributes is applied to the class that represents the table.
Specifies the maximum number of characters for a data field when users are inserting or updating data in a table.
Determines the field template to use to display a data field. You can use this attribute to associate a custom user control with a data field.
The following attributes can be used in Dynamic Data but they can be applied only to properties.
Inserts an initial value in a data field when the data is being edited.
Provides a message in the form of a tooltip when the mouse pointer is moved over a field and when the field is in edit mode.
Changes the displayed header name for a column or table.
Validating Data Fields
Dynamic Data supports data field validation based on the data model that includes the following types of validation:
Required-field validation. If a data 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. Dynamic Data enforces that the user's input value matches the intrinsic data type for a data field.
Dynamic Data enables you to customize and extend data field validation as follows:
Using attributes. You should use this approach when you want to apply additional validation to what is already provided by Dynamic Data and the default System.ComponentModel.DataAnnotations attributes are sufficient to satisfy your requirements.
Using partial class methods. You can customize validation and add your own business logic by overriding partial methods that process the changes that were made to data fields. You should use this approach when you want to add common business logic to your data model that cannot be handled by the attributes.
For more information, see How to: Customize Data Field Validation in the Data Model.
For more information about Dynamic Data, see the following topics:
Understanding URL routing. For more information, see ASP.NET Routing.
Understanding how to use the O/R Designer in a Web application to create data classes that represent a SQL Server database. For more information, see Object Relational Designer (O/R Designer).
Understanding how to use the ADO.NET Entity Data Model Designer in a Web application to create data classes that represent a database. For more information, see ADO.NET Entity Data Model Designer (Entity Designer).
Understanding LINQ-to-SQL Object-Relational mapping. For more information, see LINQ to SQL.
Understanding the ADO.NET Entity Framework. For more information, see ADO.NET Entity Framework.
Understanding partial classes and methods. For more information, see Partial Classes and Methods (C# Programming Guide).