DbContext.cs
8.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
using SqlSugar;
using System;
namespace HH_WCS_Standard
{
public class DbContext
{
#region 属性字段
//private static string _connectionString = Properties.Resources.orlConnext;
private static string _connectionString = Properties.Resources.SqlConnect2;
/// <summary>
/// 连接字符串
/// </summary>
public static string ConnectionString
{
get { return _connectionString; }
set { _connectionString = value; }
}
//private static DbType _dbType = DbType.Oracle;
private static DbType _dbType = DbType.MySql;
/// <summary>
/// 数据库类型
/// </summary>
public static DbType DbType
{
get { return _dbType; }
set { _dbType = value; }
}
private SqlSugarClient _db;
/// <summary>
/// 数据连接对象
/// </summary>
public SqlSugarClient Db
{
get { return _db; }
private set { _db = value; }
}
/// <summary>
/// 数据库上下文实例(自动关闭连接)
/// </summary>
public static DbContext Context
{
get
{
return new DbContext();
}
}
#endregion 属性字段
#region 构造函数
/// <summary>
/// 功能描述:构造函数
/// </summary>
private DbContext()
{
if (string.IsNullOrEmpty(_connectionString))
throw new ArgumentNullException("数据库连接字符串为空");
_db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = _connectionString,
DbType = _dbType,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
IsShardSameThread = true,
MoreSettings = new ConnMoreSettings()
{
//IsWithNoLockQuery = true,
IsAutoRemoveDataCache = true
}
});
}
public DbContext(string StrConnect, DbType dbType)
{
if (string.IsNullOrEmpty(StrConnect))
throw new ArgumentNullException("数据库连接字符串为空");
_db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = StrConnect,
DbType = dbType,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = true,
IsShardSameThread = true,
MoreSettings = new ConnMoreSettings()
{
//IsWithNoLockQuery = true,
IsAutoRemoveDataCache = true
}
});
}
/// <summary>
/// 功能描述:构造函数
/// </summary>
/// <param name="blnIsAutoCloseConnection">是否自动关闭连接</param>
private DbContext(bool blnIsAutoCloseConnection)
{
if (string.IsNullOrEmpty(_connectionString))
throw new ArgumentNullException("数据库连接字符串为空");
_db = new SqlSugarClient(new ConnectionConfig()
{
ConnectionString = _connectionString,
DbType = _dbType,
InitKeyType = InitKeyType.Attribute,
IsAutoCloseConnection = blnIsAutoCloseConnection,
IsShardSameThread = true,
MoreSettings = new ConnMoreSettings()
{
//IsWithNoLockQuery = true,
IsAutoRemoveDataCache = true
}
});
}
#endregion 构造函数
#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);
}
#region 根据实体类生成数据库表
/// <summary>
/// 功能描述:根据实体类生成数据库表
/// 作 者:beck.huang
/// 创建日期:2018-05-08 10:31:02
/// 任务编号:中餐
/// </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++)
{
lstTypes[i] = typeof(T);
}
}
CreateTableByEntity(blnBackupTable, lstTypes);
}
/// <summary>
/// 功能描述:根据实体类生成数据库表
/// 作 者:beck.huang
/// 创建日期:2018-05-08 10:31:14
/// 任务编号:中餐
/// </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 根据实体类生成数据库表
#endregion 实例方法
#region 静态方法
/// <summary>
/// 功能描述:获得一个DbContext
/// </summary>
/// <param name="blnIsAutoCloseConnection">是否自动关闭连接(如果为false,则使用接受时需要手动关闭Db)</param>
/// <returns>返回值</returns>
public static DbContext GetDbContext(bool blnIsAutoCloseConnection)
{
return new DbContext(blnIsAutoCloseConnection);
}
/// <summary>
/// 功能描述:设置初始化参数
/// </summary>
/// <param name="strConnectionString">连接字符串</param>
/// <param name="enmDbType">数据库类型</param>
public static void Init(string strConnectionString, DbType enmDbType = SqlSugar.DbType.MySql)
{
_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,
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 静态方法
}
}