如何添加元数据类
WCF RIA Services支持对实体类和成员的批注。批注是通过叫做元数据类的局部类来实施的。当我们想批注一个生成的实体类,并在重新生成这个实体类时不想丢失这些批注,我们使用元数据类。通过使用[MetadataTypeAttribute]属性来指定一个元数据类。
有两种方式来添加元数据类。一种是当添加一个新的Domain Service类时,指定自动生成元数据类。还可以手动添加元数据类。下面就介绍这两种方式。
通过使用Domain Service Class模板来添加元数据类
- 在服务端项目中,添加一个新建项并选择Domain Service Class模板。
- 当出现Add New Domain Service Class对话框时,选择想要公开的实体。
- 勾选Generate associated classes for metadata选框。
4. 点击OK。就会生成一个Domain service class的文件和一个元数据文件。在元数据类的名字中会包含.metadata,例如:DomainService1.metadata.cs
5. 打开元数据类并添加一些属性如下代码所示:
代码
1 [MetadataTypeAttribute(typeof(Address.AddressMetadata))]
2 public partial class Address
3 {
4 internal sealed class AddressMetadata
5 {
6 // Metadata classes are not meant to be instantiated.
7 private AddressMetadata()
8 {
9 }
10 public int AddressID;
11 [Required]
12 [StringLength(60)]
13 public string AddressLine1;
14 public string AddressLine2;
15 [Required]
16 [StringLength(30)]
17 public string City;
18 public string CountryRegion;
19 public EntityCollection<customeraddress> CustomerAddresses;
20 public DateTime ModifiedDate;
21 [Required]
22 public string PostalCode;
23 [Exclude]
24 public Guid rowguid;
25 public string StateProvince;
26 }
27 }
2 public partial class Address
3 {
4 internal sealed class AddressMetadata
5 {
6 // Metadata classes are not meant to be instantiated.
7 private AddressMetadata()
8 {
9 }
10 public int AddressID;
11 [Required]
12 [StringLength(60)]
13 public string AddressLine1;
14 public string AddressLine2;
15 [Required]
16 [StringLength(30)]
17 public string City;
18 public string CountryRegion;
19 public EntityCollection<customeraddress> CustomerAddresses;
20 public DateTime ModifiedDate;
21 [Required]
22 public string PostalCode;
23 [Exclude]
24 public Guid rowguid;
25 public string StateProvince;
26 }
27 }
手动添加元数据
- 在服务端项目中,添加一个新类。这个类的名字应与想要公开的实体类的名字一致。按照惯例,在名字用包含.metadata。
- 添加关键字partial来使类成为局部类。下面的代码示例了一个匹配名字为 Address实体类的局部类。
1 C# Copy Code
2 public partial class Address
3 {
4 }
2 public partial class Address
3 {
4 }
3. 在局部类中,创建一个internal类作为元数据类。如下:
1 public partial class Address
2 {
3 internal sealed class AddressMetadata
4 {
5 }
6 }
2 {
3 internal sealed class AddressMetadata
4 {
5 }
6 }
4. 对局部类添加[MetadataTypeAttribute属性,并包含元数据类的类型。代码如下:
1 [MetadataTypeAttribute(typeof(Address.AddressMetadata))]
2 public partial class Address
3 {
4 internal sealed class AddressMetadata
5 {
6 }
7 }
2 public partial class Address
3 {
4 internal sealed class AddressMetadata
5 {
6 }
7 }
5. 在元数据类中,添加和实体类中成员属性同名的成员属性。
6. 对成员属性添加属性批注。示例代码如下:
代码
1 [MetadataTypeAttribute(typeof(Address.AddressMetadata))]
2 public partial class Address
3 {
4 internal sealed class AddressMetadata
5 {
6 // Metadata classes are not meant to be instantiated.
7 private AddressMetadata()
8 {
9 }
10 public int AddressID;
11 [Required]
12 [StringLength(60)]
13 public string AddressLine1;
14 public string AddressLine2;
15 [Required]
16 [StringLength(30)]
17 public string City;
18 public string CountryRegion;
19 public EntityCollection<customeraddress> CustomerAddresses;
20 public DateTime ModifiedDate;
21 [Required]
22 public string PostalCode;
23 [Exclude]
24 public Guid rowguid;
25 public string StateProvince;
26 }
27 }
2 public partial class Address
3 {
4 internal sealed class AddressMetadata
5 {
6 // Metadata classes are not meant to be instantiated.
7 private AddressMetadata()
8 {
9 }
10 public int AddressID;
11 [Required]
12 [StringLength(60)]
13 public string AddressLine1;
14 public string AddressLine2;
15 [Required]
16 [StringLength(30)]
17 public string City;
18 public string CountryRegion;
19 public EntityCollection<customeraddress> CustomerAddresses;
20 public DateTime ModifiedDate;
21 [Required]
22 public string PostalCode;
23 [Exclude]
24 public Guid rowguid;
25 public string StateProvince;
26 }
27 }
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/blackant2/archive/2010/04/08/5461576.aspx