Scaffolding is a mechanism that takes the power and functionality of the existing ASP.NET page framework and enhances it by dynamically displaying pages based on the data model without a physical page behind the scenes.
Dynamic Data uses URL routing to match and handle requests. The scaffolding mechanism infers the view and the table a user wishes to see from the URL requested. The benefit of using the routing mechanism is that the URL requested does not have to match the physical path in the application.
By default, the scaffolding mechanism is turned off. Enabling the scaffolding mechanism should be done carefully because it poses a security risk because it can expose the whole data model for display, edit, insert and delete capabilities.
You can use the following approaches to enable the scaffolding mechanism:
You are required to register the data context that will use Dynamic Data features, even if you are not going to use the scaffolding mechanism. This registration is made in the global.asax file by using the RegisterContext method. The RegisterContext method accepts a ContextConfiguration object as a parameter. In order to enable the scaffolding mechanism while registering the data context, you set the ScaffoldAllTables property of the ContextConfiguration object to true. This will enable the scaffolding mechanism for the whole data model. In other words, it will expose all the tables in the data model for display, edit, insert and delete capabilities. If you must hide some tables from the scaffolding mechanism, you can achieve this by using the ScaffoldTableAttribute attribute.
The following example shows how to enable the scaffolding mechanism for all tables in the data model while registering the data context for the AdventureWorksLT database.
model.RegisterContext(GetType(AdventureWorksLTDataContext), _
New ContextConfiguration() With {.ScaffoldAllTables = True})
If you want more control on which tables are being exposed, you can use the ScaffoldTableAttribute attribute to enable or disable the scaffolding mechanism for a given table. Instead of exposing the whole data model and hiding the tables that do not have to be exposed, you can expose only the ones required by the application. In order to apply the ScaffoldTableAttribute attribute, you must create a partial class that has the same name of the entity class in the data model and then apply the attribute to the partial class.
The following example shows how to enable the scaffolding mechanism for an individual table.
Imports System.ComponentModel.DataAnnotations
<ScaffoldTable(True)> _
Partial Public Class Product
End Class
If you want more control on which data fields are being exposed, you can use the ScaffoldColumnAttribute attribute to enable or disable the scaffolding mechanism for a given data field.
By default, not all data fields are displayed by Dynamic Data. The following are some important rules that are used by Dynamic Data to display or not a data field:
If a ScaffoldColumnAttribute attribute is applied to the data field, the data field is displayed. This rule overrides all the following rules.
If a UIHintAttribute attribute is applied to the data field, the data field is displayed. This rule overrides all the following rules.
If a data field is a foreign-key field, the data field is not displayed. This occurs because Dynamic Data handles foreign-key fields in a different way and will not typically display the foreign-key field value.
If the data field is automatically generated in the database, the data field is not displayed. Typically these kind of field do not contain relevant information. Make sure that you apply the UIHintAttribute attribute to the data field, if the data field must be displayed.
If the value of the IsCustomProperty property is true, the data field is not displayed.
In order to apply the ScaffoldColumnAttribute attribute, you must create an associated metadata class where you will apply the ScaffoldColumnAttribute attribute to the data field and you must create a partial class that has the same name of the entity class in the data model. Then, you must associate these two classes by applying the MetadataTypeAttribute attribute to the partial class.
The following example shows how to hide specific data fields, PasswordHash and PasswordSalt, from the scaffolding mechanism.
Imports System.ComponentModel.DataAnnotations
<MetadataType(GetType(Customer_Metadata))> _
Partial Public Class Customer
End Class
Public Class Customer_Metadata
<ScaffoldColumn(False)> _
Public PasswordHash As Object
<ScaffoldColumn(False)> _
Public PasswordSalt As Object
End Class