Data Access has been discontinued. Please refer to this page for more information.

How to: Add Metadata Class

This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.

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.

    public partial class Customer
    {
    }
    
    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.

    public partial class Customer
    {
       internal sealed class CustomerMetadata
       {
       }
    }
    
    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.

    [System.ComponentModel.DataAnnotations.MetadataTypeAttribute( typeof( Customer.CustomerMetadata ) )]
    public partial class Customer
    {
       internal sealed class CustomerMetadata
       {
       }
    }
    
    <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.

    [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;
           }
       }
    }
    
    <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:

    [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;
           }
       }
    }
    
    <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