using Dapper.Contrib.Extensions; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; namespace IStation.DataProvider { /// /// 实体辅助类 /// public class EntityHelper { //默认主键名称 private static readonly string _defaultPrimaryKey = "ID"; /// /// 获取表名称 /// /// /// public static string GetTableName() { var objType = typeof(T); var attrs = objType.GetCustomAttributes(typeof(TableAttribute), true); if (attrs != null && attrs.Count() > 0) return (attrs[0] as TableAttribute).Name; return objType.Name; } /// /// 获取主键属性信息 /// /// /// public static string GetPrimaryKey() { var objType = typeof(T); var pros = objType.GetProperties(); var keyPro = pros.FirstOrDefault(x => x.GetCustomAttribute(typeof(KeyAttribute)) != null); if (keyPro != null) return keyPro.Name; var explicitKeyPro = pros.FirstOrDefault(x => x.GetCustomAttribute(typeof(ExplicitKeyAttribute)) != null); if (explicitKeyPro != null) return explicitKeyPro.Name; return _defaultPrimaryKey; } } }