c#自定义ORM框架-实体类扩展属性

步骤一、建立扩展属性类

实体类扩展属性要继承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
    {
        // 身份ID
        public int Id { get; set; }
        // 名字
        public string Name { get; set; }
        // 长度
        public long Length { get; set; }
    }

 1 [AttributeUsage(AttributeTargets.Property)]
 2     public class FieldAttribute : Attribute
 3     {
 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 Dome
 4     {
 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; }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值