如何添加元数据

众所周知Entity Framwork自动生成的实体上没有注释。因为是数据库拖动过来生成的,所以也不能修改其代码.因为改后还会自动生成代码覆盖掉修改的.

 

那如何添加元数据呢.

手动添加元数据

引文地址:http://blog.youkuaiyun.com/blackant2/article/details/5461576

  1. 添加一个新类。这个类的名字应与想要公开的实体类的名字一致。按照惯 例,在名字用包含.metadata。
  2. 添加关键字partial来使类成为局部类。下面的代码示例了一个匹配名字为 Address实体类的局部类。

    [c-sharp] view plaincopy

    1. C# Copy Code  
    2. public partial class Address 
    3. }  
  3. 在 局部类中,创建一个internal类作为元数据类。如下:

    [c-sharp] view plaincopy

    1. public partial class Address 
    2. internal sealed class AddressMetadata 
    3.   { 
    4.   } 
  4. 对 局部类添加[MetadataTypeAttribute属性,并包含元数据类的类型。代码如下:

    [c-sharp] view plaincopy

    1. [MetadataTypeAttribute(typeof(Address.AddressMetadata))] 
    2. public partial class Address 
    3. internal sealed class AddressMetadata 
    4.   { 
    5.   } 
    6. }  
  5. 在 元数据类中,添加和实体类中成员属性同名的成员属性。
  6. 对成员属性添加属性批注。示例代码如下:

    [c-sharp] view plaincopy

    1. [MetadataTypeAttribute(typeof(Address.AddressMetadata))] 
    2. public partial class Address 
    3. internal sealed class AddressMetadata 
    4.     { 
    5. // Metadata classes are not meant to be instantiated.
    6. private AddressMetadata() 
    7.         { 
    8.         } 
    9. public int AddressID; 
    10.         [Required] 
    11.         [StringLength(60)] 
    12. public string AddressLine1; 
    13. public string AddressLine2; 
    14.         [Required] 
    15.         [StringLength(30)] 
    16. public string City; 
    17. public string CountryRegion; 
    18. public EntityCollection<customeraddress> CustomerAddresses; 
    19. public DateTime ModifiedDate; 
    20.         [Required] 
    21. public string PostalCode; 
    22.         [Exclude] 
    23. public Guid rowguid; 
    24. public string StateProvince; 
    25.     } 

又一个例子:

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

转载于:https://my.oschina.net/wzzz/blog/217592

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值