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
|
{
|
/// <summary>
|
/// 实体辅助类
|
/// </summary>
|
public class EntityHelper
|
{
|
//默认主键名称
|
private static readonly string _defaultPrimaryKey = "ID";
|
|
/// <summary>
|
/// 获取表名称
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <returns></returns>
|
public static string GetTableName<T>()
|
{
|
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;
|
}
|
|
/// <summary>
|
/// 获取主键属性信息
|
/// </summary>
|
/// <typeparam name="T"></typeparam>
|
/// <returns></returns>
|
public static string GetPrimaryKey<T>()
|
{
|
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;
|
}
|
|
|
}
|
}
|