Поделиться через


ASP.NET Dynamic Data Model Overview

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:

  • Background

  • Displaying and Modifying Data Fields

  • Validating Data Fields

  • Additional Information

Background

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:

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.

Attribute

Description

DataTypeAttribute

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.

DisplayColumnAttribute

Changes the default parent table column that Dynamic Data uses for a foreign-key field.

Note

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.

DisplayFormatAttribute

Applies formatting to the data in a data field.

MetadataTypeAttribute

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.

RangeAttribute

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.

RegularExpressionAttribute

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.

RequiredAttribute

Specifies that the field is required. The built-in field templates for Dynamic Data use the RequiredFieldValidator control for required fields.

ScaffoldColumnAttribute

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.

ScaffoldTableAttribute

Enables or disables scaffolding of data tables in a data model. This attributes is applied to the class that represents the table.

StringLengthAttribute

Specifies the maximum number of characters for a data field when users are inserting or updating data in a table.

UIHintAttribute

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.

Attribute

Description

DefaultValueAttribute

Inserts an initial value in a data field when the data is being edited.

DescriptionAttribute

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.

DisplayNameAttribute

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.

Additional Information

For more information about Dynamic Data, see the following topics:

Back to top

See Also

Tasks

Walkthrough: Adding Dynamic Data to an Existing Web Site

Walkthrough: Adding Dynamic Data to an Existing Web Site

Concepts

ASP.NET Dynamic Data Guidelines

ASP.NET Dynamic Data Field Templates Overview

ASP.NET Dynamic Data Scaffolding and Page Templates Overview

ASP.NET Dynamic Data Overview

Reference

System.ComponentModel.DataAnnotations

Change History

Date

History

Reason

August 2008

Added new section (Dynamic Data Attributes).

Information enhancement.

July 2008

Added topic.

SP1 feature change.