众所周知Entity Framwork自动生成的实体上没有注释。因为是数据库拖动过来生成的,所以也不能修改其代码.因为改后还会自动生成代码覆盖掉修改的.
那如何添加元数据呢.
手动添加元数据
引文地址:http://blog.csdn.net/blackant2/article/details/5461576
- 添加一个新类。这个类的名字应与想要公开的实体类的名字一致。按照惯 例,在名字用包含.metadata。
- 添加关键字partial来使类成为局部类。下面的代码示例了一个匹配名字为 Address实体类的局部类。
[c-sharp] view plaincopy
- C# Copy Code
- public partial class Address
- {
- }
- 在 局部类中,创建一个internal类作为元数据类。如下:
[c-sharp] view plaincopy
- public partial class Address
- {
- internal sealed class AddressMetadata
- {
- }
- }
- 对 局部类添加[MetadataTypeAttribute属性,并包含元数据类的类型。代码如下:
[c-sharp] view plaincopy
- [MetadataTypeAttribute(typeof(Address.AddressMetadata))]
- public partial class Address
- {
- internal sealed class AddressMetadata
- {
- }
- }
- 在 元数据类中,添加和实体类中成员属性同名的成员属性。
- 对成员属性添加属性批注。示例代码如下:
[c-sharp] view plaincopy
- [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;
- [Required]
- [StringLength(60)]
- public string AddressLine1;
- public string AddressLine2;
- [Required]
- [StringLength(30)]
- public string City;
- public string CountryRegion;
- public EntityCollection<customeraddress> CustomerAddresses;
- public DateTime ModifiedDate;
- [Required]
- public string PostalCode;
- [Exclude]
- public Guid rowguid;
- public string StateProvince;
- }
- }
又一个例子:
namespace ZYBW.ProjectMIS.Models.Dealer
{
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.ServiceModel.DomainServices.Hosting;
using System.ServiceModel.DomainServices.Server;
// The MetadataTypeAttribute identifies TbDealerInfoManageMetadata as the class
// that carries additional metadata for the TbDealerInfoManage class.
[MetadataTypeAttribute(typeof(LawEnforcementType.LawEnforcementTypeMetadata))]
public partial class LawEnforcementType
{
// This class allows you to attach custom attributes to properties
// of the TbDealerInfoManage class.
//
// For example, the following marks the Xyz property as a
// required property and specifies the format for valid values:
// [Required]
// [RegularExpression("[A-Z][A-Za-z0-9]*")]
// [StringLength(32)]
// [Display(Name = "")] public string Xyz { get; set; }
internal sealed class LawEnforcementTypeMetadata
{
// Metadata classes are not meant to be instantiated.
private LawEnforcementTypeMetadata()
{ }
/// <summary>
/// 农药ID
/// </summary>
[Display(Name = "ID")]
public int ID
{
set;
get;
}
/// <summary>
/// 农药ID
/// </summary>
[Display(Name = "执法类别")]
public string Name
{
set;
get;
}
/// <summary>
/// 农药ID
/// </summary>
[Display(Name = "值")]
public string Value
{
set;
get;
}
}
}
}