¶Ô±ÈÐÂÎļþ |
| | |
| | | using SqlSugar; |
| | | using System; |
| | | using System.Collections.Generic; |
| | | using System.Linq; |
| | | using System.Text; |
| | | using System.Threading.Tasks; |
| | | using WIDESEA_Core.Const; |
| | | using WIDESEA_Core.DB; |
| | | using WIDESEA_Core.Helper; |
| | | |
| | | namespace WIDESEA_Core.Seed |
| | | { |
| | | public class DBContext |
| | | { |
| | | 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; |
| | | |
| | | /// <summary> |
| | | /// è¿æ¥å符串 |
| | | /// </summary> |
| | | public static MutiDBOperate GetMainConnectionDb() |
| | | { |
| | | MutiDBOperate mainDb = new MutiDBOperate() |
| | | { |
| | | Connection = AppSettings.app(MainDb.ConnectionString).DecryptDES(AppSecret.DB), |
| | | ConnId = MainDb.CurrentDbConnId, |
| | | DbType = DataBaseType.SqlServer |
| | | }; |
| | | |
| | | return mainDb; |
| | | } |
| | | /// <summary> |
| | | /// è¿æ¥å符串 |
| | | /// </summary> |
| | | public static string ConnectionString |
| | | { |
| | | get { return _connectionString; } |
| | | set { _connectionString = value; } |
| | | } |
| | | /// <summary> |
| | | /// æ°æ®åºç±»å |
| | | /// </summary> |
| | | public static DbType DbType |
| | | { |
| | | get { return _dbType; } |
| | | set { _dbType = value; } |
| | | } |
| | | /// <summary> |
| | | /// æ°æ®è¿æ¥å¯¹è±¡ |
| | | /// </summary> |
| | | public SqlSugarClient Db |
| | | { |
| | | get { return _db; } |
| | | private set { _db = value; } |
| | | } |
| | | |
| | | //public SqlSugarScope DbClient |
| | | //{ |
| | | // get { return _dbClient; } |
| | | // private set { _dbClient = value; } |
| | | //} |
| | | |
| | | /// <summary> |
| | | /// åè½æè¿°:æé 彿° |
| | | /// </summary> |
| | | public DBContext(ISqlSugarClient sqlSugarClient) |
| | | { |
| | | if (string.IsNullOrEmpty(_connectionString)) |
| | | throw new ArgumentNullException("æ°æ®åºè¿æ¥å符串为空"); |
| | | |
| | | _db = sqlSugarClient as SqlSugarClient; |
| | | //_db.Aop.DataExecuting = SqlSugarAop.DataExecuting; |
| | | } |
| | | |
| | | #region å®ä¾æ¹æ³ |
| | | /// <summary> |
| | | /// åè½æè¿°:è·åæ°æ®åºå¤ç对象 |
| | | /// </summary> |
| | | /// <returns>è¿åå¼</returns> |
| | | public SimpleClient<T> GetEntityDB<T>() where T : class, new() |
| | | { |
| | | return new SimpleClient<T>(_db); |
| | | } |
| | | /// <summary> |
| | | /// åè½æè¿°:è·åæ°æ®åºå¤ç对象 |
| | | /// </summary> |
| | | /// <param name="db">db</param> |
| | | /// <returns>è¿åå¼</returns> |
| | | //public SimpleClient<T> GetEntityDB<T>(SqlSugarClient db) where T : class, new() |
| | | //{ |
| | | // return new SimpleClient<T>(db); |
| | | //} |
| | | |
| | | |
| | | |
| | | #endregion |
| | | |
| | | |
| | | #region æ ¹æ®å®ä½ç±»çææ°æ®åºè¡¨ |
| | | /// <summary> |
| | | /// åè½æè¿°:æ ¹æ®å®ä½ç±»çææ°æ®åºè¡¨ |
| | | /// </summary> |
| | | /// <param name="blnBackupTable">æ¯å¦å¤ä»½è¡¨</param> |
| | | /// <param name="lstEntitys">æå®çå®ä½</param> |
| | | public void CreateTableByEntity<T>(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); |
| | | } |
| | | |
| | | /// <summary> |
| | | /// åè½æè¿°:æ ¹æ®å®ä½ç±»çææ°æ®åºè¡¨ |
| | | /// </summary> |
| | | /// <param name="blnBackupTable">æ¯å¦å¤ä»½è¡¨</param> |
| | | /// <param name="lstEntitys">æå®çå®ä½</param> |
| | | 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 éææ¹æ³ |
| | | |
| | | ///// <summary> |
| | | ///// åè½æè¿°:è·å¾ä¸ä¸ªDbContext |
| | | ///// </summary> |
| | | ///// <returns></returns> |
| | | //public static MyContext GetDbContext() |
| | | //{ |
| | | // return new MyContext(); |
| | | //} |
| | | |
| | | /// <summary> |
| | | /// åè½æè¿°:设置åå§ååæ° |
| | | /// </summary> |
| | | /// <param name="strConnectionString">è¿æ¥å符串</param> |
| | | /// <param name="enmDbType">æ°æ®åºç±»å</param> |
| | | public static void Init(string strConnectionString, DbType enmDbType = SqlSugar.DbType.SqlServer) |
| | | { |
| | | _connectionString = strConnectionString; |
| | | _dbType = enmDbType; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// åè½æè¿°:å建ä¸ä¸ªé¾æ¥é
ç½® |
| | | /// </summary> |
| | | /// <param name="blnIsAutoCloseConnection">æ¯å¦èªå¨å
³éè¿æ¥</param> |
| | | /// <param name="blnIsShardSameThread">æ¯å¦å¤¸ç±»äºå¡</param> |
| | | /// <returns>ConnectionConfig</returns> |
| | | 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() |
| | | }, |
| | | //IsShardSameThread = blnIsShardSameThread |
| | | }; |
| | | return config; |
| | | } |
| | | |
| | | /// <summary> |
| | | /// åè½æè¿°:è·åä¸ä¸ªèªå®ä¹çDB |
| | | /// </summary> |
| | | /// <param name="config">config</param> |
| | | /// <returns>è¿åå¼</returns> |
| | | public static SqlSugarClient GetCustomDB(ConnectionConfig config) |
| | | { |
| | | return new SqlSugarClient(config); |
| | | } |
| | | /// <summary> |
| | | /// åè½æè¿°:è·åä¸ä¸ªèªå®ä¹çæ°æ®åºå¤ç对象 |
| | | /// </summary> |
| | | /// <param name="sugarClient">sugarClient</param> |
| | | /// <returns>è¿åå¼</returns> |
| | | public static SimpleClient<T> GetCustomEntityDB<T>(SqlSugarClient sugarClient) where T : class, new() |
| | | { |
| | | return new SimpleClient<T>(sugarClient); |
| | | } |
| | | /// <summary> |
| | | /// åè½æè¿°:è·åä¸ä¸ªèªå®ä¹çæ°æ®åºå¤ç对象 |
| | | /// </summary> |
| | | /// <param name="config">config</param> |
| | | /// <returns>è¿åå¼</returns> |
| | | public static SimpleClient<T> GetCustomEntityDB<T>(ConnectionConfig config) where T : class, new() |
| | | { |
| | | SqlSugarClient sugarClient = GetCustomDB(config); |
| | | return GetCustomEntityDB<T>(sugarClient); |
| | | } |
| | | #endregion |
| | | } |
| | | } |