Telerik OpenAccess ORM

Telerik OpenAccess ORM Send comments on this topic.
How to: Add Metadata Class
Programmer's Guide > Quick-Start Scenarios > Silverlight > Silverlight 4 & 5 > WCF RIA Services > How to: Add Metadata Class

Glossary Item Box

WCF RIA Services supports the ability to annotate persistent classes and properties. Annotations are implemented with partial classes called metadata classes. You use metadata classes when you want to annotate generated persistent classes, but do not want to lose those annotations when the persistent class is regenerated. You mark the metadata class by applying the MetadataTypeAttribute attribute.

To add a metadata class manually:

  1. In the server project, add a new class file with the same name as the persistent class that you want to annotate. By convention, include .metadata in its file name.
  2. Add the partial keyword to make the class a partial class. The following example shows a partial class that matches the Customer persistent class from the SofiaCarRental database.
    C# Copy Code
    public partial class Customer
    {
    }
    VB.NET Copy Code
    Partial Public Class Customer
    End Class
  3. In the partial class, create an internal class that will serve as a metadata class. The following example shows the internal metadata class.
    C# Copy Code
    public partial class Customer
    {
       
    internal sealed class CustomerMetadata
       {
       }
    }
    VB.NET Copy Code
    Partial Public Class Customer
     Friend NotInheritable Class CustomerMetadata
     End Class
    End Class
  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.
    C# Copy Code
    [System.ComponentModel.DataAnnotations.MetadataTypeAttribute( typeof( Customer.CustomerMetadata ) )]
    public partial class Customer
    {
       
    internal sealed class CustomerMetadata
       {
       }
    }
    VB.NET Copy Code
    <System.ComponentModel.DataAnnotations.MetadataTypeAttribute(GetType(Customer.CustomerMetadata))>
    Partial Public Class Customer
     Friend NotInheritable Class CustomerMetadata
     End Class
    End Class
  5. In the metadata class, add properties that have the same name as the properties in the persistent class.
    C# Copy Code
    [System.ComponentModel.DataAnnotations.MetadataTypeAttribute( typeof( Customer.CustomerMetadata ) )]
    public partial class Customer
    {
       
    internal sealed class CustomerMetadata
       {
           
    public CustomerMetadata()
           {
           }
           
    public int CustomerID
           {
               get;
               set;
           }
           
    public string DrvLicNumber
           {
               get;
               set;
           }
           
    public string FullName
           {
               get;
               set;
           }
           
    public string Address
           {
               get;
               set;
           }
           
    public string Country
           {
               get;
               set;
           }
           
    public string City
           {
               get;
               set;
           }
           
    public string State
           {
               get;
               set;
           }
           
    public string ZIPCode
           {
               get;
               set;
           }
       }
    }
    VB.NET Copy Code
    <System.ComponentModel.DataAnnotations.MetadataTypeAttribute(GetType(Customer.CustomerMetadata))>
    Partial Public Class Customer
     Friend NotInheritable Class CustomerMetadata
      Public Sub New()
      End Sub
      Public Property CustomerID() As Integer
      Public Property DrvLicNumber() As String
      Public Property FullName() As String
      Public Property Address() As String
      Public Property Country() As String
      Public Property City() As String
      Public Property State() As String
      Public Property ZIPCode() As String
     End Class
    End Class
  6. Add annotation attributes to the properties, like in the following example:
    C# Copy Code
    [MetadataTypeAttribute( typeof( Customer.CustomerMetadata ) )]
    public partial class Customer
    {
       
    internal sealed class CustomerMetadata
       {
           
    public CustomerMetadata()
           {
           }
           
    public int CustomerID
           {
               get;
               set;
           }
           [System.ComponentModel.DataAnnotations.Required]
           
    public string DrvLicNumber
           {
               get;
               set;
           }
           [System.ComponentModel.DataAnnotations.StringLength( 50 )]
           
    public string FullName
           {
               get;
               set;
           }
           [System.ComponentModel.DataAnnotations.RegularExpression(
    "abc" )]
           
    public string Address
           {
               get;
               set;
           }
           
    public string Country
           {
               get;
               set;
           }
           
    public string City
           {
               get;
               set;
           }
           
    public string State
           {
               get;
               set;
           }
           
    public string ZIPCode
           {
               get;
               set;
           }
       }
    }
    VB.NET Copy Code
    <MetadataTypeAttribute(GetType(Customer.CustomerMetadata))>
    Partial Public Class Customer
     Friend NotInheritable Class CustomerMetadata
      Public Sub New()
      End Sub
      Public Property CustomerID() As Integer
      <System.ComponentModel.DataAnnotations.Required>
      Public Property DrvLicNumber() As String
      <System.ComponentModel.DataAnnotations.StringLength(50)>
      Public Property FullName() As String
      <System.ComponentModel.DataAnnotations.RegularExpression("abc")>
      Public Property Address() As String
      Public Property Country() As String
      Public Property City() As String
      Public Property State() As String
      Public Property ZIPCode() As String
     End Class
    End Class