How to: Add Metadata Classes
[ 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.

To add a metadata class by using the Domain Service Class template

  1. 1.
    In the server project, add a new item and select the Domain Service Class template.
  2. 2.
    When the Add New Domain Service Class dialog box appears, select the entities to expose in the domain service.
  3. 3.
    Select the Generate associated classes for metadata check box.
    ​
    ​
  4. 4.
    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).
  5. 5.
    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.
    1
    <MetadataTypeAttribute(GetType(Address.AddressMetadata))> _
    2
    Partial Public Class Address
    3
    ​
    4
    Friend NotInheritable Class AddressMetadata
    5
    ​
    6
    'Metadata classes are not meant to be instantiated.
    7
    Private Sub New()
    8
    MyBase.New
    9
    End Sub
    10
    ​
    11
    Public AddressID As Integer
    12
    ​
    13
    <Required()> _
    14
    <StringLength(60)> _
    15
    Public AddressLine1 As String
    16
    ​
    17
    Public AddressLine2 As String
    18
    ​
    19
    <Required()> _
    20
    <StringLength(30)> _
    21
    Public City As String
    22
    ​
    23
    Public CountryRegion As String
    24
    ​
    25
    Public CustomerAddresses As EntityCollection(Of CustomerAddress)
    26
    ​
    27
    Public ModifiedDate As DateTime
    28
    ​
    29
    <Required()> _
    30
    Public PostalCode As String
    31
    ​
    32
    <Exclude()> _
    33
    Public rowguid As Guid
    34
    ​
    35
    Public StateProvince As String
    36
    End Class
    37
    End Class
    Copied!
    1
    [MetadataTypeAttribute(typeof(Address.AddressMetadata))]
    2
    public partial class Address
    3
    {
    4
    ​
    5
    internal sealed class AddressMetadata
    6
    {
    7
    // Metadata classes are not meant to be instantiated.
    8
    private AddressMetadata()
    9
    {
    10
    }
    11
    ​
    12
    public int AddressID;
    13
    ​
    14
    [Required]
    15
    [StringLength(60)]
    16
    public string AddressLine1;
    17
    ​
    18
    public string AddressLine2;
    19
    ​
    20
    [Required]
    21
    [StringLength(30)]
    22
    public string City;
    23
    ​
    24
    public string CountryRegion;
    25
    ​
    26
    public EntityCollection<CustomerAddress> CustomerAddresses;
    27
    ​
    28
    public DateTime ModifiedDate;
    29
    ​
    30
    [Required]
    31
    public string PostalCode;
    32
    ​
    33
    [Exclude]
    34
    public Guid rowguid;
    35
    ​
    36
    public string StateProvince;
    37
    }
    38
    }
    Copied!

To add a metadata class manually

  1. 1.
    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.
  2. 2.
    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.
    1
    Partial Public Class Address
    2
    End Class
    Copied!
    1
    public partial class Address
    2
    {
    3
    }
    Copied!
  3. 3.
    In the partial class, create an internal class that will serve as the metadata class.
    The following example shows the internal metadata class.
    1
    Partial Public Class Address
    2
    Friend NotInheritable Class AddressMetadata
    3
    End Class
    4
    End Class
    Copied!
    1
    public partial class Address
    2
    {
    3
    internal sealed class AddressMetadata
    4
    {
    5
    }
    6
    }
    Copied!
  4. 4.
    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.
    1
    <MetadataTypeAttribute(GetType(Address.AddressMetadata))> _
    2
    Partial Public Class Address
    3
    Friend NotInheritable Class AddressMetadata
    4
    End Class
    5
    End Class
    Copied!
    1
    [MetadataTypeAttribute(typeof(Address.AddressMetadata))]
    2
    public partial class Address
    3
    {
    4
    internal sealed class AddressMetadata
    5
    {
    6
    }
    7
    }
    Copied!
  5. 5.
    In the metadata class, add properties that have the same name as the properties in the entity class.
  6. 6.
    Add attributes to the properties.
    The following example shows a metadata class with RoundtripOriginalAttribute, RequiredAttribute, StringLengthAttribute, and ExcludeAttribute applied to some of the properties.
    1
    <MetadataTypeAttribute(GetType(Address.AddressMetadata))> _
    2
    Partial Public Class Address
    3
    ​
    4
    Friend NotInheritable Class AddressMetadata
    5
    ​
    6
    'Metadata classes are not meant to be instantiated.
    7
    Private Sub New()
    8
    MyBase.New
    9
    End Sub
    10
    ​
    11
    Public AddressID As Integer
    12
    ​
    13
    <Required()> _
    14
    <StringLength(60)> _
    15
    Public AddressLine1 As String
    16
    ​
    17
    Public AddressLine2 As String
    18
    ​
    19
    <Required()> _
    20
    <StringLength(30)> _
    21
    Public City As String
    22
    ​
    23
    Public CountryRegion As String
    24
    ​
    25
    Public CustomerAddresses As EntityCollection(Of CustomerAddress)
    26
    ​
    27
    Public ModifiedDate As DateTime
    28
    ​
    29
    <Required()> _
    30
    Public PostalCode As String
    31
    ​
    32
    <Exclude()> _
    33
    Public rowguid As Guid
    34
    ​
    35
    Public StateProvince As String
    36
    End Class
    37
    End Class
    Copied!
    1
    [MetadataTypeAttribute(typeof(Address.AddressMetadata))]
    2
    public partial class Address
    3
    {
    4
    ​
    5
    internal sealed class AddressMetadata
    6
    {
    7
    // Metadata classes are not meant to be instantiated.
    8
    private AddressMetadata()
    9
    {
    10
    }
    11
    ​
    12
    public int AddressID;
    13
    ​
    14
    [Required]
    15
    [StringLength(60)]
    16
    public string AddressLine1;
    17
    ​
    18
    public string AddressLine2;
    19
    ​
    20
    [Required]
    21
    [StringLength(30)]
    22
    public string City;
    23
    ​
    24
    public string CountryRegion;
    25
    ​
    26
    public EntityCollection<CustomerAddress> CustomerAddresses;
    27
    ​
    28
    public DateTime ModifiedDate;
    29
    ​
    30
    [Required]
    31
    public string PostalCode;
    32
    ​
    33
    [Exclude]
    34
    public Guid rowguid;
    35
    ​
    36
    public string StateProvince;
    37
    }
    38
    }
    Copied!
Last modified 6mo ago