• 如何添加元数据


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

     

    那如何添加元数据呢.

    手动添加元数据

    引文地址:http://blog.csdn.net/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;
                }
            }
        }
    }

  • 相关阅读:
    赋值问题
    构造方法的作用
    this的使用
    三目运算符和形参的使用
    构造方法作用:给所有对象进行相同的初始化操作
    成员变量和局部变量
    相关开发的书籍名汇集
    html ---- web sql 例子
    让input表单输入框不记录输入过信息的方法
    css实现两端对齐的3种方法
  • 原文地址:https://www.cnblogs.com/suizhikuo/p/3644406.html
Copyright © 2020-2023  润新知