一、约定
数据实体必须实现 IEntity 接口,该接口定义了一个int类型的Id属性,既每个实体必须有一个名称为Id的自增主键。
若数据表的主键列名称不是Id,可以通过 [MyKey("主键列名")] 对该属性进行描述
namespace MyOrm.Commons {public interface IEntity{int Id { get; set; }} }
二、实体描述
MyTableAttribute,用于约定实体和数据表的关联关系,若实体名称和数据表名一直,可省略此描述
MyKeyAttribute,用于约定实体的主键,若数据表主键名称不是Id,可通过此描述约定Id属性映射的数据表的主键名
MyColumnAttribute,InsertIgnore=true 插入时忽略此属性;UpdateIgnore=true 更新时忽略此属性;Ignore=true 插入和更新时忽略该属性;ColumnName 若属性名和列名不一致,使用此属性约定映射的数据表列名
MyForeignKeyAttribute,ForeignKey 用于约定导航属性和外键列,若外键名不为 “导航属性名 + Id”,则需要通过此描述进行约定。
using System;namespace MyOrm.Attributes {[AttributeUsage(AttributeTargets.Class)]public class MyTableAttribute : Attribute{public string TableName { get; }public MyTableAttribute(string tableName){TableName = tableName;}}[AttributeUsage(AttributeTargets.Property)]public class MyKeyAttribute : Attribute{public bool IsIncrement { get; set; } = true;public string FieldName { get; set; }}[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field)]public class MyColumnAttribute : Attribute{public string ColumnName { get; set; }public bool Ignore { get; set; }public bool InsertIgnore { get; set; }public bool UpdateIgnore { get; set; }}[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]public class MyForeignKeyAttribute : Attribute{public string ForeignKey { get; set; }public string MasterKey { get; set; } = "Id";public MyForeignKeyAttribute(string foreignKey){ForeignKey = foreignKey;}} }
三、示例:
using MyOrm.Attributes; using MyOrm.Commons; using System;namespace ConsoleApp1 {// 实体对应的数据表名为 Base_Student[MyTable("Base_Student")]public class Student : IEntity{// 数据表的主键名称为StudentId[MyKey(FieldName = "StudentId")]public int Id { get; set; }public string StudentName { get; set; }public string Mobile { get; set; }public string Card { get; set; }public string State { get; set; }public DateTime? Birthday { get; set; }// 更新时忽略学校Id[MyColumn(UpdateIgnore = true)]public int FKSchoolId { get; set; }// 是否删除字段只能通过 Delete 方法修改,Update时需忽略该属性[MyColumn(UpdateIgnore = true)]public bool IsDel { get; set; }// 创建时间和创建人只在创建时指定,其他时候不能修改[MyColumn(UpdateIgnore = true)]public DateTime CreateAt { get; set; }[MyColumn(UpdateIgnore = true)]public string CreateBy { get; set; }public DateTime UpdateAt { get; set; }public string UpdateBy { get; set; }// 导航属性的外键为 FKSchoolId,若不指定,默认为SchoolId[MyForeignKey("FKSchoolId")]public School School { get; set; }} }