How to: Add Metadata Classes
Last updated
Last updated
[ This document was written for WCF Services Version 1 Service Pack 2 and might not be up to date Please see Release Notes or Changelog for a list of changes since WCF RIA Services ]
This topic explains how to use metadata to add functionality to your server and client side entities by decorating properties in a metadata class with attributes that you want to affect the behavior of your entities. Open Ria Services supports the ability to annotate entity classes and properties. Annotations are implemented with partial classes called metadata classes. You use metadata classes when you want to annotate the generated entity classes, but do not want to lose those annotations when the entity class is regenerated. This allows you to add functionality such as validation and child entity composition. You specify a metadata class by using the MetadataTypeAttribute attribute.
There are two ways that you can add metadata classes. When you add a new DomainService class with the Add New Domain Service Class wizard, you can specify that a metadata class be automatically generated by checking the Generate associated classes for metadata. You can also add a metadata class manually. This topic describes these two ways to add metadata classes.
In the server project, add a new item and select the Domain Service Class template.
When the Add New Domain Service Class dialog box appears, select the entities to expose in the domain service.
Select the Generate associated classes for metadata check box.
Click OK.
A file for the domain service class is generated, and a file for the metadata class is generated. The metadata class contains .metadata in its name (such as, DomainService1.metadata.cs or DomainService1.metadata.vb).
Open the metadata class and add attributes to the properties.
The following example shows a metadata class with RoundtripOriginalAttribute, RequiredAttribute, StringLengthAttribute, and ExcludeAttribute applied to some of the properties.
In the server project, add a new class file with the same name as the entity class that you want to annotate. By convention, include .metadata in its file name.
Add the partial keyword to make the class a partial class.
The following example shows a partial class that matches an entity class named Address.
In the partial class, create an internal class that will serve as the metadata class.
The following example shows the internal metadata class.
Add a MetadataTypeAttribute attribute to the partial class and include the type of the metadata class.
The following example shows the MetadataTypeAttribute attribute applied to the class.
In the metadata class, add properties that have the same name as the properties in the entity class.
Add attributes to the properties.
The following example shows a metadata class with RoundtripOriginalAttribute, RequiredAttribute, StringLengthAttribute, and ExcludeAttribute applied to some of the properties.