Files
zszq-trs/YLErpDAL/DataBase/EFExtensions/TableMapper.cs
T
2024-05-09 14:06:26 +08:00

37 lines
1.1 KiB
C#

using System.Collections.Concurrent;
namespace BaseOUDAL.EFExtensions
{
/// <summary>
/// Used to store table names
/// </summary>
// ReSharper disable once UnusedMember.Global
public static class TableMapper
{
private static readonly ConcurrentDictionary<RuntimeTypeHandle, string> TableNames = new ConcurrentDictionary<RuntimeTypeHandle, string>();
internal static string GetTableName(Type type)
{
if (TableNames.TryGetValue(type.TypeHandle, out var name))
{
return name;
}
var tableAttr = type.GetCustomAttributes(false).SingleOrDefault(attr => attr.GetType().Name == "TableAttribute") as dynamic;
if (tableAttr != null)
{
name = tableAttr.Name;
}
else
{
name = type.IsInterface && type.Name.StartsWith("I")
? type.Name.Substring(1)
: type.Name;
}
TableNames[type.TypeHandle] = name;
return name;
}
}
}