步骤一、建立扩展属性类
实体类扩展属性要继承Attribute基类完成
步骤二、创建实体类并引用扩展实体类属性
Attributes属性定义,主要标明表名和主键名字
/// <summary>
/// 表名
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
[System.Serializable]
public class TableNameAttribute : Attribute
{public object _value { get; private set; }public TableNameAttribute(string tableName){this._value = tableName;}
}/// <summary>
/// 主键名
/// </summary>
[AttributeUsage(AttributeTargets.Class)]
[System.Serializable]
public class PrimaryKeyAttribute : Attribute
{public object _value { get; private set; }public PrimaryKeyAttribute(string primaryKey){this._value = primaryKey;}
}// 使用方式(例)/// <summary>/// 学生类/// </summary>[TableName("MapRecord")][PrimaryKey("ID")]public class Student{// 身份IDpublic int Id { get; set; }// 名字public string Name { get; set; }// 长度public long Length { get; set; }}
1 [AttributeUsage(AttributeTargets.Property)]2 public class FieldAttribute : Attribute3 {4 /// <summary>5 ///是否为主键(true/false)6 /// </summary>7 public bool PropertyKey { get; set; }8 /// <summary>9 /// 是否为自动增长(true/false) 10 /// </summary> 11 public bool Identity { get; set; } 12 }
1 [AttributeUsage(AttributeTargets.Class)] 2 public class TableAttribute:Attribute 3 { 4 /// <summary> 5 /// 数据库表名 6 /// </summary> 7 public string TableName { get; set; } 8 }
[Table(TableName = "emesc.Dome")]2 [Serializable]3 public class Dome4 {5 [Field(PropertyKey = true)]6 public string EMP_NO { get; set; }7 public string EMP_NAME { get; set; }8 public string EMP_DESC { get; set; }9 public string TYPE { get; set; } 10 public string EMP_RANK { get; set; } 11 public string EMP_PASS { get; set; } 12 public object FACTORY_CODE { get; set; } 13 public DateTime QUIT_DATE { get; set; } 14 public object CALENDAR_CODE { get; set; } 15 }
/// <summary>/// 主键特性(在实体类的上方加这个特性,指定该类的主键名称)/// </summary>[AttributeUsage(AttributeTargets.Class)]public class PrimaryKeyAttribute : Attribute{private PrimaryKeyAttribute(){}private string _name;/// <summary>/// 构造方法/// </summary>/// <param name="name"></param>public PrimaryKeyAttribute(string name){_name = name;}/// <summary>/// 主键名称/// </summary>public string Name{get { return _name; }}}