Files
HTCloud/HT.Cloud.Data/DBContext/DBContexHelper.cs
2023-03-03 16:07:50 +08:00

65 lines
1.9 KiB
C#

using SqlSugar;
using System;
using System.Linq;
using HT.Cloud.Code;
namespace HT.Cloud.DataBase
{
public class DBContexHelper
{
private static string defaultDbType = GlobalContext.SystemConfig.DBProvider;
private static string defaultDbConnectionString = GlobalContext.SystemConfig.DBConnectionString;
public static ConnectionConfig Contex(string ConnectStr = "", string providerName = "")
{
ConnectStr = string.IsNullOrEmpty(ConnectStr) ? defaultDbConnectionString : ConnectStr;
providerName = string.IsNullOrEmpty(providerName) ? defaultDbType : providerName;
var dbType = Convert.ToInt32(Enum.Parse(typeof(DbType), providerName));
if (dbType == Convert.ToInt32(DbType.SqlServer))
{
return new ConnectionConfig()
{
DbType = (DbType)dbType,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
ConnectionString = ConnectStr,
MoreSettings = new ConnMoreSettings()
{
IsAutoRemoveDataCache = true//自动清理缓存
},
ConfigureExternalServices = new ConfigureExternalServices()
{
DataInfoCacheService = new SqlSugarCache(), //配置我们创建的缓存类
EntityService = (property, column) =>
{
var attributes = property.GetCustomAttributes(true);//get all attributes
if (attributes.Any(it => it is SugarColumn) && column.DataType == "longtext")
{
column.DataType = "nvarchar(4000)";
}
}
}
};
}
else
{
return new ConnectionConfig()
{
DbType = (DbType)dbType,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
ConnectionString = ConnectStr,
ConfigureExternalServices = new ConfigureExternalServices()
{
DataInfoCacheService = new SqlSugarCache() //配置我们创建的缓存类
},
MoreSettings = new ConnMoreSettings()
{
IsAutoRemoveDataCache = true//自动清理缓存
}
};
}
}
}
}