奋斗的博客

开源、创新!

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  34 随笔 :: 0 文章 :: 164 评论 :: 95539 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

    上一篇中完成了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; }
        }        
    }
}

    在IdAttribute类上的属性配置:

[AttributeUsage(AttributeTargets.Field|AttributeTargets.Property, AllowMultiple = false, Inherited = false)]

    在这里AllowMultiple ,Inherited 在前面自己动手写ORM 框架(三):关系映射配置—Table属性中已经介绍,这里不罗嗦了。
AttributeTargets.Field表示Id属性可以配置在私有成员Field上,代码块1-3:

[Table(name="Student")]
public class StudentEntity
{
    [Id(Name = "studentid", Strategy = GenerationType.SEQUENCE)]  
    private string stuid;                        
      
    //[Id(Name = "studentid", Strategy = GenerationType.SEQUENCE)]  
    public string Stuid
    {
        get { return stuid; }
        set { stuid = value; }
    }
}

    这里我们将[Id]加在了private string stuid;上面了,和加在属性上是一样的。

AttributeTargets.Property自然就是表示Id属性可以配置在属性Property上了,1-3中Stuid

上注释部分//[Id].

AttributeTargets.Field|AttributeTargets.Property中间加上 | 表示或的意思,即你可以配置在Field或者Property上都可以。

    这里还用到了一个GenerationType类,效果和枚举一样,源码如下:

using System;
using System.Collections.Generic;
using System.Text;

namespace System.Orm.CustomAttributes
{
    public class GenerationType 
    {        
        public const int INDENTITY = 1;//自动增长
         public const int SEQUENCE = 2;//序列
         public const int TABLE = 3;//TABLE

        private GenerationType() { }//私有构造函数,不可被实例化对象
    }
}

       好了,到这里Id自定义属性就完成了。

Technorati 标签: ORM
posted on   奋斗  阅读(3022)  评论(13编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示