You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
Tiobon.Web.Core/Tiobon.Core.Common/DB/EntityUtility.cs

51 lines
1.5 KiB

using SqlSugar;
using System.Diagnostics;
using System.Reflection;
using Tiobon.Core.Extensions;
using Tiobon.Core.DB;
using Tiobon.Core.Model;
namespace Tiobon.Core.Common.DB;
public class EntityUtility
{
private static readonly Lazy<Dictionary<string, List<Type>>> _tenantEntitys = new(() =>
{
Dictionary<string, List<Type>> dic = new Dictionary<string, List<Type>>();
var assembly = Assembly.Load("Tiobon.Core.Model");
//扫描 实体
foreach (var type in assembly.GetTypes().Where(s => s.IsClass && !s.IsAbstract))
{
var tenant = type.GetCustomAttribute<TenantAttribute>();
if (tenant != null)
{
dic.TryAdd(tenant.configId.ToString(), type);
continue;
}
if (type.IsSubclassOf(typeof(RootEntityTkey<>)))
{
dic.TryAdd(MainDb.CurrentDbConnId, type);
continue;
}
var table = type.GetCustomAttribute<SugarTable>();
if (table != null)
{
dic.TryAdd(MainDb.CurrentDbConnId, type);
continue;
}
Debug.Assert(type.Namespace != null, "type.Namespace != null");
if (type.Namespace.StartsWith("Tiobon.Core.Model.Models"))
{
dic.TryAdd(MainDb.CurrentDbConnId, type);
continue;
}
}
return dic;
});
public static Dictionary<string, List<Type>> TenantEntitys => _tenantEntitys.Value;
}