Learn.VideoAnalysis/VideoAnalysisCore/Common/Repository.cs

45 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using SqlSugar;
using SqlSugar.IOC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using VideoAnalysisCore.Model;
namespace VideoAnalysisCore.Common
{
public class Repository<T> : SimpleClient<T> where T : class, new()
{
readonly Dictionary<Type, object?> CID = new Dictionary<Type, object?>();
public Repository()
{
SwitchConnection();
}
public void SwitchConnection()
{
var t = typeof(T);
if (CID.ContainsKey(t))
{
base.Context = CID[t] != null
? DbScoped.Sugar.GetConnectionScope(CID[t])
: DbScoped.Sugar;
return;
}
else
{
var c = t.GetCustomAttribute<TenantAttribute>();
if (!CID.ContainsKey(t))
CID.Add(t, c?.configId);
base.Context = c != null
? DbScoped.Sugar.GetConnectionScope(c.configId)
: DbScoped.Sugar;
//这个变量也要保证是线程安全的尽量CopyNew在方法中使用
base.Context = base.Context.CopyNew();
}
}
}
}