sqlsugar/Src/Asp.Net/SqlSugar/Abstract/CodeFirstProvider/SplitCodeFirstProvider.cs

72 lines
2.3 KiB
C#
Raw Normal View History

2025-05-11 16:20:50 +08:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace SqlSugar
{
public class SplitCodeFirstProvider
{
public SqlSugarProvider Context;
public int DefaultLength { get; set; }
public void InitTables<T>()
{
var type = typeof(T);
InitTables(type);
}
public void InitTables(Type type)
{
var isSplitEntity = type.GetCustomAttribute<SplitTableAttribute>() != null;
if (isSplitEntity)
{
UtilMethods.StartCustomSplitTable(this.Context, type);
_InitTables(type);
UtilMethods.EndCustomSplitTable(this.Context, type);
}
else
{
this.Context.CodeFirst.SetStringDefaultLength(this.DefaultLength).InitTables(type);
}
}
private void _InitTables(Type type)
{
//var oldMapping = this.Context.Utilities.TranslateCopy(this.Context.MappingTables);
SplitTableContext helper = new SplitTableContext(Context)
{
EntityInfo = this.Context.EntityMaintenance.GetEntityInfo(type)
};
helper.CheckPrimaryKey();
var tables = helper.GetTables();
//var oldMapingTables = this.Context.MappingTables;
if (tables.Count > 0)
{
foreach (var item in tables)
{
this.Context.MappingTables.Add(helper.EntityInfo.EntityName, item.TableName);
this.Context.CodeFirst.SetStringDefaultLength(this.DefaultLength).InitTables(type);
}
}
else
{
this.Context.MappingTables.Add(helper.EntityInfo.EntityName, helper.GetDefaultTableName());
this.Context.CodeFirst.SetStringDefaultLength(this.DefaultLength).InitTables(type);
}
this.Context.MappingTables.Add(helper.EntityInfo.EntityName, helper.EntityInfo.DbTableName);
}
public void InitTables(Type [] types)
{
foreach (var type in types)
{
InitTables(type);
}
}
}
}