using SqlSugar;
using System;
namespace Admin.Core.Common
{
public class MyContext
{
private static MutiDBOperate connectObject => GetMainConnectionDb();
private static string _connectionString = connectObject.Connection;
private static DbType _dbType = (DbType)connectObject.DbType;
public static string ConnId = connectObject.ConnId;
private SqlSugarClient _db;
///
/// 连接字符串
/// Admin.Core
///
public static MutiDBOperate GetMainConnectionDb()
{
var mainConnetctDb = BaseDBConfig.MutiConnectionString.allDbs.Find(x => x.ConnId == MainDb.CurrentDbConnId);
if (BaseDBConfig.MutiConnectionString.allDbs.Count > 0)
{
if (mainConnetctDb == null)
{
mainConnetctDb = BaseDBConfig.MutiConnectionString.allDbs[0];
}
}
else
{
throw new Exception("请确保appsettigns.json中配置连接字符串,并设置Enabled为true;");
}
return mainConnetctDb;
}
///
/// 连接字符串
/// Admin.Core
///
public static string ConnectionString
{
get { return _connectionString; }
set { _connectionString = value; }
}
///
/// 数据库类型
/// Admin.Core
///
public static DbType DbType
{
get { return _dbType; }
set { _dbType = value; }
}
///
/// 数据连接对象
/// Admin.Core
///
public SqlSugarClient Db
{
get { return _db; }
private set { _db = value; }
}
///
/// 功能描述:构造函数
/// 作 者:Admin.Core
///
public MyContext(ISqlSugarClient sqlSugarClient)
{
if (string.IsNullOrEmpty(_connectionString))
throw new ArgumentNullException("数据库连接字符串为空");
_db = sqlSugarClient as SqlSugarClient;
}
#region 实例方法
///
/// 功能描述:获取数据库处理对象
/// 作 者:Admin.Core
///
/// 返回值
public SimpleClient GetEntityDB() where T : class, new()
{
return new SimpleClient(_db);
}
///
/// 功能描述:获取数据库处理对象
/// 作 者:Admin.Core
///
/// db
/// 返回值
public SimpleClient GetEntityDB(SqlSugarClient db) where T : class, new()
{
return new SimpleClient(db);
}
#endregion
#region 根据实体类生成数据库表
///
/// 功能描述:根据实体类生成数据库表
/// 作 者:Admin.Core
///
/// 是否备份表
/// 指定的实体
public void CreateTableByEntity(bool blnBackupTable, params T[] lstEntitys) where T : class, new()
{
Type[] lstTypes = null;
if (lstEntitys != null)
{
lstTypes = new Type[lstEntitys.Length];
for (int i = 0; i < lstEntitys.Length; i++)
{
T t = lstEntitys[i];
lstTypes[i] = typeof(T);
}
}
CreateTableByEntity(blnBackupTable, lstTypes);
}
///
/// 功能描述:根据实体类生成数据库表
/// 作 者:Admin.Core
///
/// 是否备份表
/// 指定的实体
public void CreateTableByEntity(bool blnBackupTable, params Type[] lstEntitys)
{
if (blnBackupTable)
{
_db.CodeFirst.BackupTable().InitTables(lstEntitys); //change entity backupTable
}
else
{
_db.CodeFirst.InitTables(lstEntitys);
}
}
#endregion
#region 静态方法
/////
///// 功能描述:获得一个DbContext
///// 作 者:Admin.Core
/////
/////
//public static MyContext GetDbContext()
//{
// return new MyContext();
//}
///
/// 功能描述:设置初始化参数
/// 作 者:Admin.Core
///
/// 连接字符串
/// 数据库类型
public static void Init(string strConnectionString, DbType enmDbType = SqlSugar.DbType.SqlServer)
{
_connectionString = strConnectionString;
_dbType = enmDbType;
}
///
/// 功能描述:创建一个链接配置
/// 作 者:Admin.Core
///
/// 是否自动关闭连接
/// 是否夸类事务
/// ConnectionConfig
public static ConnectionConfig GetConnectionConfig(bool blnIsAutoCloseConnection = true, bool blnIsShardSameThread = false)
{
ConnectionConfig config = new ConnectionConfig()
{
ConnectionString = _connectionString,
DbType = _dbType,
IsAutoCloseConnection = blnIsAutoCloseConnection,
ConfigureExternalServices = new ConfigureExternalServices()
{
//DataInfoCacheService = new HttpRuntimeCache()
}
};
return config;
}
///
/// 功能描述:获取一个自定义的DB
/// 作 者:Admin.Core
///
/// config
/// 返回值
public static SqlSugarClient GetCustomDB(ConnectionConfig config)
{
return new SqlSugarClient(config);
}
///
/// 功能描述:获取一个自定义的数据库处理对象
/// 作 者:Admin.Core
///
/// sugarClient
/// 返回值
public static SimpleClient GetCustomEntityDB(SqlSugarClient sugarClient) where T : class, new()
{
return new SimpleClient(sugarClient);
}
///
/// 功能描述:获取一个自定义的数据库处理对象
/// 作 者:Admin.Core
///
/// config
/// 返回值
public static SimpleClient GetCustomEntityDB(ConnectionConfig config) where T : class, new()
{
SqlSugarClient sugarClient = GetCustomDB(config);
return GetCustomEntityDB(sugarClient);
}
#endregion
}
}