15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1256 ❘ ChaPTer 42 Asp.net dynAmic dAtA And mvc<br />

ScaffoldAllTables property of the ContextConfiguration for the sites to true, as illustrated in this<br />

code for the LINQ to SQL site:<br />

DefaultModel.RegisterContext(typeof(MagicShopDataContext),<br />

new ContextConfiguration() { ScaffoldAllTables = true });<br />

code snippet PCSDynamicDataDemoLinq/Global.asax<br />

If the value is changed to false, then the default behavior is not to provide any scaffolding for any tables or<br />

columns. In order to instruct the dynamic data framework to scaffold a table of column, you must provide<br />

metadata in your data model. This metadata will then be read by the dynamic data runtime, <strong>and</strong> scaffolding<br />

will be generated for you. Metadata can also provide information for other things — such as validation<br />

logic — as you will see shortly.<br />

To supply metadata for a table, you must do two things:<br />

➤<br />

➤<br />

Create a metadata class definition for each table you want to supply metadata for, with members that<br />

map to columns.<br />

Associate the metadata classes with the data model table classes.<br />

All data model items, in both LINQ to SQL <strong>and</strong> ADO.<strong>NET</strong> Entities, are generated for you as partial<br />

class definitions. In the sample code, for example, there is a class called Customer (contained in the<br />

MagicShopModel namespace in the ADO.<strong>NET</strong> Entities version) that is used for rows in the Customer table.<br />

To supply metadata for Customer rows, you might create a class called CustomerMetadata. Once you<br />

have done that, you can supply a second partial class definition for Customer <strong>and</strong> use the MetadataType<br />

attribute to link the classes together.<br />

In the .<strong>NET</strong> Framework, metadata is supported through data annotations. As such it should come as no<br />

surprise that the MetadataType attribute, along with other metadata attributes, is found in the System<br />

.ComponentModel.DataAnnotations namespace. The MetadataType attribute uses a Type parameter<br />

to specify the metadata type. The two attributes that control scaffolding are ScaffoldTable <strong>and</strong><br />

ScaffoldColumn. Both of these attributes have a Boolean parameter to specify whether scaffolding will be<br />

generated for a table or column.<br />

The following code shows an example of how this is achieved:<br />

using System.ComponentModel.DataAnnotations;<br />

...<br />

[MetadataType(typeof(CustomerMetadata))]<br />

public partial class Customer { }<br />

using System.ComponentModel.DataAnnotations;<br />

...<br />

[ScaffoldTable(true)]<br />

public class CustomerMetadata<br />

{<br />

[ScaffoldColumn(false)]<br />

public object Address { get; set; }<br />

}<br />

code snippets PCSDynamicDataDemoEntities/App_Code/Customer.cs <strong>and</strong><br />

PCSDynamicDataDemoLinq/App_Code/Customer.cs<br />

code snippets PCSDynamicDataDemoEntities/App_Code/CustomerMetadata.cs <strong>and</strong><br />

PCSDynamicDataDemoLinq/App_Code/CustomerMetadata.cs<br />

Here, the ScaffoldTable attribute specifies that scaffolding will be generated for the Customer table, <strong>and</strong><br />

the ScaffoldColumn attribute is used to ensure that the Address column will have no scaffolding. Note<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!