You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.4 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Estsh.Core.Dapper
{
public static class EntityExtensions
{
public static string? GetEntityColumnName(this Type type, string propertyName)
{
var entityProperty = type.GetProperties().FirstOrDefault(p => p.Name.ToLower() == propertyName.ToLower());
if (entityProperty != null)
{
var entityColumn = entityProperty.GetCustomAttributes<ColumnAttribute>().FirstOrDefault();
if (entityColumn != null)
{
return entityColumn.Name;
}
}
return propertyName;
}
public static string? GetEntityTableName(this Type type)
{
return type.GetCustomAttribute<TableAttribute>(false)?.Name;
}
/// <summary>
/// 属性判断待完善
/// </summary>
/// <returns></returns>
public static IEnumerable<PropertyInfo> GetEntityProperties(this Type type)
{
return type.GetProperties().Where(
x => !x.PropertyType.IsGenericType
&& x.PropertyType.GetInterface("IList") == null
|| x.PropertyType.GetInterface("IEnumerable", false) == null);
}
}
}