首页 / 网页编程 / ASP.NET / 自己动手写ASP.NET ORM框架(四):关系映射配置—Id属性
        
            自己动手写ASP.NET ORM框架(四):关系映射配置—Id属性2011-04-24 博客园 奋斗上一篇中完成了Table自定义属性的功能,现在来完成Id,因为一张表最主要的是结构就 是表名(Table name)、主键(Id)、列(Column)、主键生成策略。Id自定义属性的用法代码块1-1:[Table(name="Student")]
public class StudentEntity
{
   private string stuid;
   [Id(Name = "studentid", Strategy = GenerationType.SEQUENCE)]
   public string Stuid
   {
     get { return stuid; }
     set { stuid = value; }
   }
}在Stuid属性上[Id]就表示在StudentEntity实体类中,Stuid属性字段对应Student表中主 键ID,Name = "studentid"表示该属性对应Student表中的studentid列名,如果Name值未指 定或者为空,将默认为该属性名称和对应的Student表中的列名相同,都为Stuid。Strategy = GenerationType.SEQUENCE表示主键生成方式,这里是自动增长。下面是自定义属性Id的完整代码块1-2:using System;
using System.Collections.Generic;
using System.Text;
namespace System.Orm.CustomAttributes
{
   [AttributeUsage(AttributeTargets.Field|AttributeTargets.Property,
     AllowMultiple = false, Inherited = false)]
   public class IdAttribute : Attribute
   {
     private string _Name = string.Empty;
     private int _Strategy = GenerationType.INDENTITY;
     public string Name
     {
       get { return _Name; }
       set { _Name = value; }
     }
     public int Strategy
     {
       get { return _Strategy; }
       set { _Strategy = value; }
     }
   }
}