如何:添加元数据类

本主题说明如何通过使用您要影响实体行为的特性来修饰元数据类中的属性,使用元数据向您的服务器和客户端实体添加功能。WCF RIA Services 支持对实体类和属性进行批注。批注使用称作元数据类的分部类实现。在您想要对生成的实体类进行批注、但在重新生成该实体类时又不想丢失这些批注时,可以使用元数据类。这使您可以添加验证和子实体组合之类的功能。您通过使用 MetadataTypeAttribute 特性来指定元数据类。

有两种方法可以添加元数据类。在您使用**“添加新的域服务类”向导添加新的 DomainService 类时,可以通过选中“为元数据生成关联类”**,指定自动生成元数据类。您还可以手动添加元数据类。本主题介绍用于添加元数据类的这两个方法。

通过使用“域服务类”模板添加元数据类

  1. 在服务器项目中,添加一个新项并且选择**“域服务类”**模板。

  2. 在**“添加新的域服务类”**对话框出现后,选择要在域服务中公开的实体。

  3. 选中**“为元数据生成关联类”**复选框。

    RIA_ServicesMetadataClass

  4. 单击**“确定”**。

    此时将为域服务类生成一个文件,并且为元数据类生成一个文件。该元数据类在其名称中包含 .metadata(例如 DomainService1.metadata.csDomainService1.metadata.vb)。

  5. 打开元数据类,并向属性添加特性。

    下面的示例演示了一个元数据类,对它的某些属性应用了 RoundtripOriginalAttributeRequiredAttributeStringLengthAttributeExcludeAttribute

    <MetadataTypeAttribute(GetType(Address.AddressMetadata))>  _
    Partial Public Class Address
    
        Friend NotInheritable Class AddressMetadata
    
            'Metadata classes are not meant to be instantiated.
            Private Sub New()
                MyBase.New
            End Sub
    
            Public AddressID As Integer
    
            <Required()> _
            <StringLength(60)> _
            <RoundtripOriginal()> _
            Public AddressLine1 As String
    
            <RoundtripOriginal()> _
            Public AddressLine2 As String
    
            <Required()> _
            <StringLength(30)> _
            <RoundtripOriginal()> _
            Public City As String
    
            <RoundtripOriginal()> _
            Public CountryRegion As String
    
            Public CustomerAddresses As EntityCollection(Of CustomerAddress)
    
            <RoundtripOriginal()> _
            Public ModifiedDate As DateTime
    
            <Required()> _
            <RoundtripOriginal()> _
            Public PostalCode As String
    
            <Exclude()> _
            Public rowguid As Guid
    
            <RoundtripOriginal()> _
            Public StateProvince As String
        End Class
    End Class
    
    [MetadataTypeAttribute(typeof(Address.AddressMetadata))]
    public partial class Address
    {
    
        internal sealed class AddressMetadata
        {
            // Metadata classes are not meant to be instantiated.
            private AddressMetadata()
            {
            }
    
            public int AddressID { get; set; }
    
            [Required]
            [StringLength(60)]
            [RoundtripOriginal]
            public string AddressLine1 { get; set; }
    
            [RoundtripOriginal]
            public string AddressLine2 { get; set; }
    
            [Required]
            [StringLength(30)]
            [RoundtripOriginal]
            public string City { get; set; }
    
            [RoundtripOriginal]
            public string CountryRegion { get; set; }
    
            public EntityCollection<CustomerAddress> CustomerAddresses { get; set; }
    
            [RoundtripOriginal]
            public DateTime ModifiedDate { get; set; }
    
            [Required]
            [RoundtripOriginal]
            public string PostalCode { get; set; }
    
            [Exclude]
            public Guid rowguid { get; set; }
    
            [RoundtripOriginal]
            public string StateProvince { get; set; }
        }
    }
    

手动添加元数据类

  1. 在服务器项目中,添加一个新的类文件,其名称与您要进行批注的实体类的名称相同。按照约定,在其文件名中包含 .metadata

  2. 添加 partial 关键字以便使该类成为分部类。

    下面的示例演示与名为 Address 的实体类匹配的一个分部类。

    Partial Public Class Address
    End Class
    
    public partial class Address
    {
    }
    
  3. 在该分部类中,创建一个将用作元数据类的内部类。

    下面的示例演示该内部元数据类。

    Partial Public Class Address
      Friend NotInheritable Class AddressMetadata
      End Class
    End Class
    
    public partial class Address
    {
      internal sealed class AddressMetadata
      {
      }
    }
    
  4. 向该分部类添加一个 MetadataTypeAttribute 特性并且包括该元数据类的类型。

    下面的示例演示了应用于该类的 MetadataTypeAttribute 特性。

    <MetadataTypeAttribute(GetType(Address.AddressMetadata))> _
    Partial Public Class Address
      Friend NotInheritable Class AddressMetadata
      End Class
    End Class
    
    [MetadataTypeAttribute(typeof(Address.AddressMetadata))]
    public partial class Address
    {
      internal sealed class AddressMetadata
      {
      }
    }
    
  5. 在元数据类中,添加与实体类中的属性同名的属性。

  6. 向这些属性添加特性。

    下面的示例演示了一个元数据类,对它的某些属性应用了 RoundtripOriginalAttributeRequiredAttributeStringLengthAttributeExcludeAttribute

    <MetadataTypeAttribute(GetType(Address.AddressMetadata))>  _
    Partial Public Class Address
    
        Friend NotInheritable Class AddressMetadata
    
            'Metadata classes are not meant to be instantiated.
            Private Sub New()
                MyBase.New
            End Sub
    
            Public AddressID As Integer
    
            <Required()> _
            <StringLength(60)> _
            <RoundtripOriginal()> _
            Public AddressLine1 As String
    
            <RoundtripOriginal()> _
            Public AddressLine2 As String
    
            <Required()> _
            <StringLength(30)> _
            <RoundtripOriginal()> _
            Public City As String
    
            <RoundtripOriginal()> _
            Public CountryRegion As String
    
            Public CustomerAddresses As EntityCollection(Of CustomerAddress)
    
            <RoundtripOriginal()> _
            Public ModifiedDate As DateTime
    
            <Required()> _
            <RoundtripOriginal()> _
            Public PostalCode As String
    
            <Exclude()> _
            Public rowguid As Guid
    
            <RoundtripOriginal()> _
            Public StateProvince As String
        End Class
    End Class
    
    [MetadataTypeAttribute(typeof(Address.AddressMetadata))]
    public partial class Address
    {
    
        internal sealed class AddressMetadata
        {
            // Metadata classes are not meant to be instantiated.
            private AddressMetadata()
            {
            }
    
            public int AddressID { get; set; }
    
            [Required]
            [StringLength(60)]
            [RoundtripOriginal]
            public string AddressLine1 { get; set; }
    
            [RoundtripOriginal]
            public string AddressLine2 { get; set; }
    
            [Required]
            [StringLength(30)]
            [RoundtripOriginal]
            public string City { get; set; }
    
            [RoundtripOriginal]
            public string CountryRegion { get; set; }
    
            public EntityCollection<CustomerAddress> CustomerAddresses { get; set; }
    
            [RoundtripOriginal]
            public DateTime ModifiedDate { get; set; }
    
            [Required]
            [RoundtripOriginal]
            public string PostalCode { get; set; }
    
            [Exclude]
            public Guid rowguid { get; set; }
    
            [RoundtripOriginal]
            public string StateProvince { get; set; }
        }
    }