接口:

  1. using Common;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Data.Common;
  7. using System.Data.Entity;
  8. using System.Data.Entity.Infrastructure;
  9. using System.Linq;
  10. using System.Linq.Expressions;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13.  
  14. namespace Service
  15. {
  16. public interface IRepository<T> where T : class
  17. {
  18. #region 执行sql语句返回datatable
  19. object SqlQueryForScalar(string sql, DbParameter[] parameters);
  20. DataTable SqlQueryForDataTatable(string sql, DbParameter[] parameters);
  21. #endregion
  22.  
  23. #region 获取单条记录
  24. /// <summary>
  25. /// 通过lambda表达式获取一条记录p=>p.id==id
  26. /// </summary>
  27. T Get(Expression<Func<T, bool>> predicate);
  28.  
  29. #endregion
  30.  
  31. #region 单模型 CRUD 操作
  32. /// <summary>
  33. /// 增加一条记录
  34. /// </summary>
  35. /// <param name="entity">实体模型</param>
  36. /// <param name="IsCommit">是否提交(默认提交)</param>
  37. /// <returns></returns>
  38. bool Save(T entity, bool IsCommit = true);
  39. /// <summary>
  40. /// 增加一条记录(异步方式)
  41. /// </summary>
  42. /// <param name="entity">实体模型</param>
  43. /// <param name="IsCommit">是否提交(默认提交)</param>
  44. /// <returns></returns>
  45. Task<bool> SaveAsync(T entity, bool IsCommit = true);
  46.  
  47. /// <summary>
  48. /// 更新一条记录
  49. /// </summary>
  50. /// <param name="entity">实体模型</param>
  51. /// <param name="IsCommit">是否提交(默认提交)</param>
  52. /// <returns></returns>
  53. bool Update(T entity, bool IsCommit = true);
  54. /// <summary>
  55. /// 更新一条记录(异步方式)
  56. /// </summary>
  57. /// <param name="entity">实体模型</param>
  58. /// <param name="IsCommit">是否提交(默认提交)</param>
  59. /// <returns></returns>
  60. Task<bool> UpdateAsync(T entity, bool IsCommit = true);
  61.  
  62. /// <summary>
  63. /// 增加或更新一条记录
  64. /// </summary>
  65. /// <param name="entity">实体模型</param>
  66. /// <param name="IsSave">是否增加</param>
  67. /// <param name="IsCommit">是否提交(默认提交)</param>
  68. /// <returns></returns>
  69. bool SaveOrUpdate(T entity, bool IsSave, bool IsCommit = true);
  70. /// <summary>
  71. /// 增加或更新一条记录(异步方式)
  72. /// </summary>
  73. /// <param name="entity">实体模型</param>
  74. /// <param name="IsSave">是否增加</param>
  75. /// <param name="IsCommit">是否提交(默认提交)</param>
  76. /// <returns></returns>
  77. Task<bool> SaveOrUpdateAsync(T entity, bool IsSave, bool IsCommit = true);
  78.  
  79. #endregion
  80.  
  81. #region 多模型操作
  82. /// <summary>
  83. /// 增加多条记录,同一模型
  84. /// </summary>
  85. /// <param name="T1">实体模型集合</param>
  86. /// <param name="IsCommit">是否提交(默认提交)</param>
  87. /// <returns></returns>
  88. bool SaveList(List<T> T1, bool IsCommit = true);
  89. /// <summary>
  90. /// 增加多条记录,同一模型(异步方式)
  91. /// </summary>
  92. /// <param name="T1">实体模型集合</param>
  93. /// <param name="IsCommit">是否提交(默认提交)</param>
  94. /// <returns></returns>
  95. Task<bool> SaveListAsync(List<T> T1, bool IsCommit = true);
  96.  
  97. /// <summary>
  98. /// 增加多条记录,独立模型
  99. /// </summary>
  100. /// <param name="T1">实体模型集合</param>
  101. /// <param name="IsCommit">是否提交(默认提交)</param>
  102. /// <returns></returns>
  103. bool SaveList<T1>(List<T1> T, bool IsCommit = true) where T1 : class;
  104. /// <summary>
  105. /// 增加多条记录,独立模型(异步方式)
  106. /// </summary>
  107. /// <param name="T1">实体模型集合</param>
  108. /// <param name="IsCommit">是否提交(默认提交)</param>
  109. /// <returns></returns>
  110. Task<bool> SaveListAsync<T1>(List<T1> T, bool IsCommit = true) where T1 : class;
  111.  
  112. /// <summary>
  113. /// 更新多条记录,同一模型
  114. /// </summary>
  115. /// <param name="T1">实体模型集合</param>
  116. /// <param name="IsCommit">是否提交(默认提交)</param>
  117. /// <returns></returns>
  118. bool UpdateList(List<T> T1, bool IsCommit = true);
  119. /// <summary>
  120. /// 更新多条记录,同一模型(异步方式)
  121. /// </summary>
  122. /// <param name="T1">实体模型集合</param>
  123. /// <param name="IsCommit">是否提交(默认提交)</param>
  124. /// <returns></returns>
  125. Task<bool> UpdateListAsync(List<T> T1, bool IsCommit = true);
  126.  
  127. /// <summary>
  128. /// 更新多条记录,独立模型
  129. /// </summary>
  130. /// <param name="T1">实体模型集合</param>
  131. /// <param name="IsCommit">是否提交(默认提交)</param>
  132. /// <returns></returns>
  133. bool UpdateList<T1>(List<T1> T, bool IsCommit = true) where T1 : class;
  134. /// <summary>
  135. /// 更新多条记录,独立模型(异步方式)
  136. /// </summary>
  137. /// <param name="T1">实体模型集合</param>
  138. /// <param name="IsCommit">是否提交(默认提交)</param>
  139. /// <returns></returns>
  140. Task<bool> UpdateListAsync<T1>(List<T1> T, bool IsCommit = true) where T1 : class;
  141.  
  142. /// <summary>
  143. /// 删除多条记录,同一模型
  144. /// </summary>
  145. /// <param name="T1">实体模型集合</param>
  146. /// <param name="IsCommit">是否提交(默认提交)</param>
  147. /// <returns></returns>
  148. bool DeleteList(List<T> T1, bool IsCommit = true);
  149. /// <summary>
  150. /// 删除多条记录,同一模型(异步方式)
  151. /// </summary>
  152. /// <param name="T1">实体模型集合</param>
  153. /// <param name="IsCommit">是否提交(默认提交)</param>
  154. /// <returns></returns>
  155. Task<bool> DeleteListAsync(List<T> T1, bool IsCommit = true);
  156.  
  157. /// <summary>
  158. /// 删除多条记录,独立模型
  159. /// </summary>
  160. /// <param name="T1">实体模型集合</param>
  161. /// <param name="IsCommit">是否提交(默认提交)</param>
  162. /// <returns></returns>
  163. bool DeleteList<T1>(List<T1> T, bool IsCommit = true) where T1 : class;
  164. /// <summary>
  165. /// 删除多条记录,独立模型(异步方式)
  166. /// </summary>
  167. /// <param name="T1">实体模型集合</param>
  168. /// <param name="IsCommit">是否提交(默认提交)</param>
  169. /// <returns></returns>
  170. Task<bool> DeleteListAsync<T1>(List<T1> T, bool IsCommit = true) where T1 : class;
  171.  
  172. /// <summary>
  173. /// 通过Lamda表达式,删除一条或多条记录
  174. /// </summary>
  175. /// <param name="predicate"></param>
  176. /// <param name="IsCommit"></param>
  177. /// <returns></returns>
  178. bool Delete(Expression<Func<T, bool>> predicate, bool IsCommit = true);
  179. /// <summary>
  180. /// 通过Lamda表达式,删除一条或多条记录(异步方式)
  181. /// </summary>
  182. /// <param name="predicate"></param>
  183. /// <param name="IsCommit"></param>
  184. /// <returns></returns>
  185. Task<bool> DeleteAsync(Expression<Func<T, bool>> predicate, bool IsCommit = true);
  186.  
  187. /// <summary>
  188. /// 执行SQL删除
  189. /// </summary>
  190. /// <param name="sql">SQL语句</param>
  191. /// <param name="para">Parameters参数</param>
  192. int DeleteBySql(string sql, params DbParameter[] para);
  193. /// <summary>
  194. /// 执行SQL删除(异步方式)
  195. /// </summary>
  196. /// <param name="sql">SQL语句</param>
  197. /// <param name="para">Parameters参数</param>
  198. Task<int> DeleteBySqlAsync(string sql, params DbParameter[] para);
  199. #endregion
  200.  
  201. #region 存储过程操作
  202. /// <summary>
  203. /// 执行增删改存储过程
  204. /// </summary>
  205. object ExecuteProc(string procname, params DbParameter[] parameter);
  206. /// <summary>
  207. /// 执行查询的存储过程
  208. /// </summary>
  209. object ExecuteQueryProc(string procname, params DbParameter[] parameter);
  210. #endregion
  211.  
  212. #region 获取多条数据操作
  213.  
  214. /// <summary>
  215. /// 返回IQueryable集合,延时加载数据
  216. /// </summary>
  217. /// <param name="predicate"></param>
  218. /// <returns></returns>
  219. IQueryable<T> LoadAll(Expression<Func<T, bool>> predicate);
  220. /// <summary>
  221. /// 返回IQueryable集合,延时加载数据(异步方式)
  222. /// </summary>
  223. /// <param name="predicate"></param>
  224. /// <returns></returns>
  225. Task<IQueryable<T>> LoadAllAsync(Expression<Func<T, bool>> predicate);
  226. /// <summary>
  227. /// 返回List<T>集合,不采用延时加载
  228. /// </summary>
  229. /// <param name="predicate"></param>
  230. /// <returns></returns>
  231. List<T> LoadListAll(Expression<Func<T, bool>> predicate);
  232. /// <summary>
  233. /// 返回List<T>集合,不采用延时加载(异步方式)
  234. /// </summary>
  235. /// <param name="predicate"></param>
  236. /// <returns></returns>
  237. Task<List<T>> LoadListAllAsync(Expression<Func<T, bool>> predicate);
  238. /// <summary>
  239. /// 获取DbQuery的列表
  240. /// </summary>
  241. /// <param name="predicate"></param>
  242. /// <returns></returns>
  243. DbQuery<T> LoadQueryAll(Expression<Func<T, bool>> predicate);
  244. /// <summary>
  245. /// 获取DbQuery的列表(异步方式)
  246. /// </summary>
  247. /// <param name="predicate"></param>
  248. /// <returns></returns>
  249. Task<DbQuery<T>> LoadQueryAllAsync(Expression<Func<T, bool>> predicate);
  250. /// <summary>
  251. /// 获取IEnumerable列表
  252. /// </summary>
  253. /// <param name="sql">SQL语句</param>
  254. /// <param name="para">Parameters参数</param>
  255. /// <returns></returns>
  256. IEnumerable<T> LoadEnumerableAll(string sql, params DbParameter[] para);
  257. /// <summary>
  258. /// 获取IEnumerable列表(异步方式)
  259. /// </summary>
  260. /// <param name="sql">SQL语句</param>
  261. /// <param name="para">Parameters参数</param>
  262. /// <returns></returns>
  263. Task<IEnumerable<T>> LoadEnumerableAllAsync(string sql, params DbParameter[] para);
  264. /// <summary>
  265. /// 获取数据动态集合
  266. /// </summary>
  267. /// <param name="sql">SQL语句</param>
  268. /// <param name="para">Parameters参数</param>
  269. /// <returns></returns>
  270. IEnumerable LoadEnumerable(string sql, params DbParameter[] para);
  271. /// <summary>
  272. /// 获取数据动态集合(异步方式)
  273. /// </summary>
  274. /// <param name="sql">SQL语句</param>
  275. /// <param name="para">Parameters参数</param>
  276. /// <returns></returns>
  277. Task<IEnumerable> LoadEnumerableAsync(string sql, params DbParameter[] para);
  278. /// <summary>
  279. /// 采用SQL进行数据的查询,返回IList集合
  280. /// </summary>
  281. /// <param name="sql">SQL语句</param>
  282. /// <param name="para">Parameters参数</param>
  283. /// <returns></returns>
  284. List<T> SelectBySql(string sql, params DbParameter[] para);
  285. /// <summary>
  286. /// 采用SQL进行数据的查询,返回IList集合(异步方式)
  287. /// </summary>
  288. /// <param name="sql">SQL语句</param>
  289. /// <param name="para">Parameters参数</param>
  290. /// <returns></returns>
  291. Task<List<T>> SelectBySqlAsync(string sql, params DbParameter[] para);
  292. /// <summary>
  293. /// 采用SQL进行数据的查询,指定泛型,返回IList集合
  294. /// </summary>
  295. /// <typeparam name="T1"></typeparam>
  296. /// <param name="sql"></param>
  297. /// <param name="para"></param>
  298. /// <returns></returns>
  299. List<T1> SelectBySql<T1>(string sql, params DbParameter[] para);
  300. /// <summary>
  301. /// 采用SQL进行数据的查询,指定泛型,返回IList集合
  302. /// </summary>
  303. /// <typeparam name="T1"></typeparam>
  304. /// <param name="sql"></param>
  305. /// <param name="para"></param>
  306. /// <returns></returns>
  307. Task<List<T1>> SelectBySqlAsync<T1>(string sql, params DbParameter[] para);
  308. /// <summary>
  309. /// 可指定返回结果、排序、查询条件的通用查询方法,返回实体对象集合
  310. /// </summary>
  311. /// <typeparam name="TEntity">实体对象</typeparam>
  312. /// <typeparam name="TOrderBy">排序字段类型</typeparam>
  313. /// <typeparam name="TResult">数据结果,与TEntity一致</typeparam>
  314. /// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
  315. /// <param name="orderby">排序字段</param>
  316. /// <param name="selector">返回结果(必须是模型中存在的字段)</param>
  317. /// <param name="IsAsc">排序方向,true为正序false为倒序</param>
  318. /// <returns>实体集合</returns>
  319. List<TResult> QueryEntity<TEntity, TOrderBy, TResult>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Expression<Func<TEntity, TResult>> selector, bool IsAsc)
  320. where TEntity : class
  321. where TResult : class;
  322. /// <summary>
  323. /// 可指定返回结果、排序、查询条件的通用查询方法,返回实体对象集合(异步方式)
  324. /// </summary>
  325. /// <typeparam name="TEntity">实体对象</typeparam>
  326. /// <typeparam name="TOrderBy">排序字段类型</typeparam>
  327. /// <typeparam name="TResult">数据结果,与TEntity一致</typeparam>
  328. /// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
  329. /// <param name="orderby">排序字段</param>
  330. /// <param name="selector">返回结果(必须是模型中存在的字段)</param>
  331. /// <param name="IsAsc">排序方向,true为正序false为倒序</param>
  332. /// <returns>实体集合</returns>
  333. Task<List<TResult>> QueryEntityAsync<TEntity, TOrderBy, TResult>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Expression<Func<TEntity, TResult>> selector, bool IsAsc)
  334. where TEntity : class
  335. where TResult : class;
  336.  
  337. /// <summary>
  338. /// 可指定返回结果、排序、查询条件的通用查询方法,返回Object对象集合
  339. /// </summary>
  340. /// <typeparam name="TEntity">实体对象</typeparam>
  341. /// <typeparam name="TOrderBy">排序字段类型</typeparam>
  342. /// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
  343. /// <param name="orderby">排序字段</param>
  344. /// <param name="selector">返回结果(必须是模型中存在的字段)</param>
  345. /// <param name="IsAsc">排序方向,true为正序false为倒序</param>
  346. /// <returns>自定义实体集合</returns>
  347. List<object> QueryObject<TEntity, TOrderBy>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Func<IQueryable<TEntity>, List<object>> selector, bool IsAsc)
  348. where TEntity : class;
  349. /// <summary>
  350. /// 可指定返回结果、排序、查询条件的通用查询方法,返回Object对象集合(异步方式)
  351. /// </summary>
  352. /// <typeparam name="TEntity">实体对象</typeparam>
  353. /// <typeparam name="TOrderBy">排序字段类型</typeparam>
  354. /// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
  355. /// <param name="orderby">排序字段</param>
  356. /// <param name="selector">返回结果(必须是模型中存在的字段)</param>
  357. /// <param name="IsAsc">排序方向,true为正序false为倒序</param>
  358. /// <returns>自定义实体集合</returns>
  359. Task<List<object>> QueryObjectAsync<TEntity, TOrderBy>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Func<IQueryable<TEntity>, List<object>> selector, bool IsAsc)
  360. where TEntity : class;
  361.  
  362. /// <summary>
  363. /// 可指定返回结果、排序、查询条件的通用查询方法,返回动态类对象集合
  364. /// </summary>
  365. /// <typeparam name="TEntity">实体对象</typeparam>
  366. /// <typeparam name="TOrderBy">排序字段类型</typeparam>
  367. /// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
  368. /// <param name="orderby">排序字段</param>
  369. /// <param name="selector">返回结果(必须是模型中存在的字段)</param>
  370. /// <param name="IsAsc">排序方向,true为正序false为倒序</param>
  371. /// <returns>动态类</returns>
  372. dynamic QueryDynamic<TEntity, TOrderBy>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Func<IQueryable<TEntity>, List<object>> selector, bool IsAsc)
  373. where TEntity : class;
  374. /// <summary>
  375. /// 可指定返回结果、排序、查询条件的通用查询方法,返回动态类对象集合(异步方式)
  376. /// </summary>
  377. /// <typeparam name="TEntity">实体对象</typeparam>
  378. /// <typeparam name="TOrderBy">排序字段类型</typeparam>
  379. /// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
  380. /// <param name="orderby">排序字段</param>
  381. /// <param name="selector">返回结果(必须是模型中存在的字段)</param>
  382. /// <param name="IsAsc">排序方向,true为正序false为倒序</param>
  383. /// <returns>动态类</returns>
  384. Task<dynamic> QueryDynamicAsync<TEntity, TOrderBy>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Func<IQueryable<TEntity>, List<object>> selector, bool IsAsc)
  385. where TEntity : class;
  386.  
  387. #endregion
  388.  
  389. #region 分页查询
  390. Common.PageInfo<object> FindAll<TEntity>(int PageIndex, int PageSize, Expression<Func<TEntity, bool>> condition, String orderByExpression, bool IsDESC)
  391. where TEntity : class;
  392. /// <summary>
  393. /// 通用EF分页,默认显示20条记录
  394. /// </summary>
  395. /// <typeparam name="TEntity">实体模型</typeparam>
  396. /// <typeparam name="TOrderBy">排序类型</typeparam>
  397. /// <param name="index">当前页</param>
  398. /// <param name="pageSize">显示条数</param>
  399. /// <param name="where">过滤条件</param>
  400. /// <param name="orderby">排序字段</param>
  401. /// <param name="selector">结果集合</param>
  402. /// <param name="isAsc">排序方向true正序 false倒序</param>
  403. /// <returns>自定义实体集合</returns>
  404. PageInfo<object> Query<TEntity, TOrderBy>
  405. (int index, int pageSize,
  406. Expression<Func<TEntity, bool>> where,
  407. Expression<Func<TEntity, TOrderBy>> orderby,
  408. Func<IQueryable<TEntity>, List<object>> selector,
  409. bool IsAsc)
  410. where TEntity : class;
  411. /// <summary>
  412. /// 对IQueryable对象进行分页逻辑处理,过滤、查询项、排序对IQueryable操作
  413. /// </summary>
  414. /// <param name="t">Iqueryable</param>
  415. /// <param name="index">当前页</param>
  416. /// <param name="PageSize">每页显示多少条</param>
  417. /// <returns>当前IQueryable to List的对象</returns>
  418. Common.PageInfo<T> Query(IQueryable<T> query, int index, int PageSize);
  419. /// <summary>
  420. /// 普通SQL查询分页方法
  421. /// </summary>
  422. /// <param name="index">当前页</param>
  423. /// <param name="pageSize">显示行数</param>
  424. /// <param name="tableName">表名/视图</param>
  425. /// <param name="field">获取项</param>
  426. /// <param name="filter">过滤条件</param>
  427. /// <param name="orderby">排序字段+排序方向</param>
  428. /// <param name="group">分组字段</param>
  429. /// <returns>结果集</returns>
  430. Common.PageInfo Query(int index, int pageSize, string tableName, string field, string filter, string orderby, string group, params DbParameter[] para);
  431. /// <summary>
  432. /// 简单的Sql查询分页
  433. /// </summary>
  434. /// <param name="index"></param>
  435. /// <param name="pageSize"></param>
  436. /// <param name="sql"></param>
  437. /// <returns></returns>
  438. Common.PageInfo Query(int index, int pageSize, string sql, string orderby, params DbParameter[] para);
  439. /// <summary>
  440. /// 多表联合分页算法
  441. /// </summary>
  442. PageInfo Query(IQueryable query, int index, int pagesize);
  443. #endregion
  444.  
  445. #region ADO.NET增删改查方法
  446. /// <summary>
  447. /// 执行增删改方法,含事务处理
  448. /// </summary>
  449. object ExecuteSqlCommand(string sql, params DbParameter[] para);
  450. /// <summary>
  451. /// 执行多条SQL,增删改方法,含事务处理
  452. /// </summary>
  453. object ExecuteSqlCommand(Dictionary<string, object> sqllist);
  454. /// <summary>
  455. /// 执行查询方法,返回动态类,接收使用var,遍历时使用dynamic类型
  456. /// </summary>
  457. object ExecuteSqlQuery(string sql, params DbParameter[] para);
  458. #endregion
  459.  
  460. #region 更新操作
  461. /// <summary>
  462. /// 更新字段
  463. /// </summary>
  464. /// <param name="table">表名</param>
  465. /// <param name="dic">被解析的字段</param>
  466. /// <param name="where">条件</param>
  467. /// <returns></returns>
  468. //bool Modify(string table, Dictionary<string, object> dic, string where);
  469. #endregion
  470.  
  471. #region 验证是否存在
  472.  
  473. /// <summary>
  474. /// 验证当前条件是否存在相同项
  475. /// </summary>
  476. bool IsExist(Expression<Func<T, bool>> predicate);
  477. /// <summary>
  478. /// 验证当前条件是否存在相同项(异步方式)
  479. /// </summary>
  480. Task<bool> IsExistAsync(Expression<Func<T, bool>> predicate);
  481. /// <summary>
  482. /// 根据SQL验证实体对象是否存在
  483. /// </summary>
  484. bool IsExist(string sql, params DbParameter[] para);
  485. /// <summary>
  486. /// 根据SQL验证实体对象是否存在(异步方式)
  487. /// </summary>
  488. Task<bool> IsExistAsync(string sql, params DbParameter[] para);
  489. #endregion
  490. }
  491. }

实现:

  1. using Domain;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Data;
  6. using System.Data.Common;
  7. using System.Data.Entity;
  8. using System.Data.Entity.Core.Objects;
  9. using System.Data.Entity.Infrastructure;
  10. using System.Data.SqlClient;
  11. using System.Linq;
  12. using System.Linq.Expressions;
  13. using System.Text;
  14. using System.Threading.Tasks;
  15.  
  16. namespace Service
  17. {
  18. public abstract class RepositoryBase<T> : IRepository<T> where T : class
  19. {
  20.  
  21. #region 执行sql语句返回datatable/object
  22. /// <summary>
  23. /// 执行查询并返回首行首列的值
  24. /// </summary>
  25. /// <param name="sql"></param>
  26. /// <param name="parameters"></param>
  27. /// <returns></returns>
  28. public object SqlQueryForScalar(string sql, DbParameter[] parameters)
  29. {
  30. string connStr = this.Context.Database.Connection.ConnectionString;
  31. using (SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr))
  32. {
  33. if (conn.State != ConnectionState.Open)
  34. {
  35. conn.Open();
  36. }
  37.  
  38. SqlCommand cmd = new SqlCommand();
  39. cmd.Connection = conn;
  40. cmd.CommandText = sql;
  41. cmd.CommandType = CommandType.Text;
  42.  
  43. if (parameters != null && parameters.Length > )
  44. {
  45. foreach (var item in parameters)
  46. {
  47. cmd.Parameters.Add(item);
  48. }
  49. }
  50. object obj = cmd.ExecuteScalar();
  51.  
  52. conn.Close();//连接需要关闭
  53. conn.Dispose();
  54. return obj;
  55. }
  56. }
  57. /// <summary>
  58. /// 执行查询并返回DataTable
  59. /// </summary>
  60. /// <param name="sql"></param>
  61. /// <param name="parameters"></param>
  62. /// <returns></returns>
  63. public DataTable SqlQueryForDataTatable(string sql, DbParameter[] parameters)
  64. {
  65. string connStr = this.Context.Database.Connection.ConnectionString;
  66. using (SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr))
  67. {
  68. if (conn.State != ConnectionState.Open)
  69. {
  70. conn.Open();
  71. }
  72. SqlCommand cmd = new SqlCommand();
  73. cmd.Connection = conn;
  74. cmd.CommandText = sql;
  75. cmd.CommandType = CommandType.Text;
  76.  
  77. if (parameters != null && parameters.Length > )
  78. {
  79. foreach (var item in parameters)
  80. {
  81. cmd.Parameters.Add(item);
  82. }
  83. }
  84.  
  85. SqlDataAdapter adapter = new SqlDataAdapter(cmd);
  86. DataTable table = new DataTable();
  87. adapter.Fill(table);
  88. return table;
  89. }
  90. }
  91. #endregion
  92.  
  93. #region 获取单条记录
  94. /// <summary>
  95. /// 通过lambda表达式获取一条记录p=>p.id==id
  96. /// </summary>
  97. public virtual T Get(Expression<Func<T, bool>> predicate)
  98. {
  99. try
  100. {
  101. return dbSet.AsNoTracking().SingleOrDefault(predicate);
  102. }
  103. catch (Exception e)
  104. {
  105. throw e;
  106. }
  107. }
  108. #endregion
  109.  
  110. #region 单模型 CRUD 操作
  111. /// <summary>
  112. /// 增加一条记录
  113. /// </summary>
  114. /// <param name="entity">实体模型</param>
  115. /// <param name="IsCommit">是否提交(默认提交)</param>
  116. /// <returns></returns>
  117. public virtual bool Save(T entity, bool IsCommit = true)
  118. {
  119. try
  120. {
  121. int row = ;
  122. var entry = this.Context.Entry<T>(entity);
  123. entry.State = System.Data.Entity.EntityState.Added;
  124. if (IsCommit)
  125. {
  126. row = Context.SaveChanges();
  127. entry.State = System.Data.Entity.EntityState.Detached;
  128. }
  129. return row > ;
  130. }
  131. catch (Exception e)
  132. {
  133. throw e;
  134. }
  135. }
  136. /// <summary>
  137. /// 增加一条记录(异步方式)
  138. /// </summary>
  139. /// <param name="entity">实体模型</param>
  140. /// <param name="IsCommit">是否提交(默认提交)</param>
  141. /// <returns></returns>
  142. public virtual Task<bool> SaveAsync(T entity, bool IsCommit = true)
  143. {
  144. try
  145. {
  146. int row = ;
  147. var entry = this.Context.Entry<T>(entity);
  148. entry.State = System.Data.Entity.EntityState.Added;
  149. if (IsCommit)
  150. {
  151. row = Context.SaveChanges();
  152. entry.State = System.Data.Entity.EntityState.Detached;
  153. }
  154. return Task.FromResult<bool>(row > );
  155. }
  156. catch (Exception e)
  157. {
  158. throw e;
  159. }
  160. }
  161.  
  162. /// <summary>
  163. /// 更新一条记录
  164. /// </summary>
  165. /// <param name="entity">实体模型</param>
  166. /// <param name="IsCommit">是否提交(默认提交)</param>
  167. /// <returns></returns>
  168. public virtual bool Update(T entity, bool IsCommit = true)
  169. {
  170. try
  171. {
  172. int rows = ;
  173. var entry = this.Context.Entry(entity);
  174. entry.State = System.Data.Entity.EntityState.Modified;
  175. if (IsCommit)
  176. {
  177. rows = this.Context.SaveChanges();
  178. entry.State = System.Data.Entity.EntityState.Detached;
  179. }
  180. return rows > ;
  181. }
  182. catch (Exception e)
  183. {
  184. throw e;
  185. }
  186. }
  187. /// <summary>
  188. /// 更新一条记录(异步方式)
  189. /// </summary>
  190. /// <param name="entity">实体模型</param>
  191. /// <param name="IsCommit">是否提交(默认提交)</param>
  192. /// <returns></returns>
  193. public virtual Task<bool> UpdateAsync(T entity, bool IsCommit = true)
  194. {
  195. try
  196. {
  197. int rows = ;
  198. var entry = this.Context.Entry(entity);
  199. entry.State = System.Data.Entity.EntityState.Modified;
  200. if (IsCommit)
  201. {
  202. rows = this.Context.SaveChanges();
  203. entry.State = System.Data.Entity.EntityState.Detached;
  204. }
  205. return Task.FromResult<bool>(rows > );
  206. }
  207. catch (Exception e)
  208. {
  209. throw e;
  210. }
  211. }
  212.  
  213. /// <summary>
  214. /// 增加或更新一条记录
  215. /// </summary>
  216. /// <param name="entity">实体模型</param>
  217. /// <param name="IsSave">是否增加</param>
  218. /// <param name="IsCommit">是否提交(默认提交)</param>
  219. /// <returns></returns>
  220. public virtual bool SaveOrUpdate(T entity, bool IsSave, bool IsCommit = true)
  221. {
  222. try
  223. {
  224. return IsSave ? Save(entity, IsCommit) : Update(entity, IsCommit);
  225. }
  226. catch (Exception e) { throw e; }
  227. }
  228. /// <summary>
  229. /// 增加或更新一条记录(异步方式)
  230. /// </summary>
  231. /// <param name="entity">实体模型</param>
  232. /// <param name="IsSave">是否增加</param>
  233. /// <param name="IsCommit">是否提交(默认提交)</param>
  234. /// <returns></returns>
  235. public virtual Task<bool> SaveOrUpdateAsync(T entity, bool IsSave, bool IsCommit = true)
  236. {
  237. try
  238. {
  239. return IsSave ? SaveAsync(entity, IsCommit) : UpdateAsync(entity, IsCommit);
  240. }
  241. catch (Exception e) { throw e; }
  242. }
  243.  
  244. /// <summary>
  245. /// 更新字段
  246. /// </summary>
  247. /// <param name="table">表名</param>
  248. /// <param name="dic">被解析的字段</param>
  249. /// <param name="where">条件</param>
  250. /// <returns></returns>
  251. //public virtual bool Modify(string table, Dictionary<string, object> dic, string where)
  252. //{
  253. // try
  254. // {
  255. // string sql = "update "+table+" ";
  256. // return this.Context.Database.ExecuteSqlCommand(sql, para);
  257. // }
  258. // catch (Exception e)
  259. // {
  260. // throw e;
  261. // }
  262. //}
  263. #endregion
  264.  
  265. #region 多模型操作
  266.  
  267. /// <summary>
  268. /// 增加多模型数据,指定独立模型集合
  269. /// </summary>
  270. public virtual int SaveList<T1>(List<T1> t) where T1 : class
  271. {
  272. try
  273. {
  274. if (t == null || t.Count == ) return ;
  275. this.Context.Set<T1>().Local.Clear();
  276. foreach (var item in t)
  277. {
  278. this.Context.Set<T1>().Add(item);
  279. }
  280. return this.Context.SaveChanges();
  281. }
  282. catch (Exception e)
  283. {
  284. throw e;
  285. }
  286. }
  287. /// <summary>
  288. /// 增加多模型数据,与当前模型一致
  289. /// </summary>
  290. public virtual int SaveList(List<T> t)
  291. {
  292. try
  293. {
  294. this.dbSet.Local.Clear();
  295. foreach (var item in t)
  296. {
  297. this.dbSet.Add(item);
  298. }
  299. return this.Context.SaveChanges();
  300. }
  301. catch (Exception e)
  302. {
  303. throw e;
  304. }
  305. }
  306.  
  307. /// <summary>
  308. /// 增加多条记录,同一模型
  309. /// </summary>
  310. /// <param name="T1">实体模型集合</param>
  311. /// <param name="IsCommit">是否提交(默认提交)</param>
  312. /// <returns></returns>
  313. public virtual bool SaveList(List<T> T1, bool IsCommit = true)
  314. {
  315. try
  316. {
  317. if (T1 == null || T1.Count == ) return false;
  318. this.Context.Set<T>().Local.Clear();
  319. foreach (var item in T1)
  320. {
  321. this.Context.Set<T>().Add(item);
  322. }
  323. if (IsCommit)
  324. {
  325. return this.Context.SaveChanges() > ;
  326. }
  327. return false;
  328. }
  329. catch (Exception e)
  330. {
  331. throw e;
  332. }
  333. }
  334. /// <summary>
  335. /// 增加多条记录,同一模型(异步方式)
  336. /// </summary>
  337. /// <param name="T1">实体模型集合</param>
  338. /// <param name="IsCommit">是否提交(默认提交)</param>
  339. /// <returns></returns>
  340. public virtual Task<bool> SaveListAsync(List<T> T1, bool IsCommit = true)
  341. {
  342. try
  343. {
  344. if (T1 == null || T1.Count == ) return Task.FromResult<bool>(false);
  345. this.Context.Set<T>().Local.Clear();
  346. foreach (var item in T1)
  347. {
  348. this.Context.Set<T>().Add(item);
  349. }
  350. if (IsCommit)
  351. {
  352. return Task.FromResult<bool>(this.Context.SaveChanges() > );
  353. }
  354. return Task.FromResult<bool>(false);
  355. }
  356. catch (Exception e)
  357. {
  358. throw e;
  359. }
  360. }
  361.  
  362. /// <summary>
  363. /// 增加多条记录,独立模型
  364. /// </summary>
  365. /// <param name="T1">实体模型集合</param>
  366. /// <param name="IsCommit">是否提交(默认提交)</param>
  367. /// <returns></returns>
  368. public virtual bool SaveList<T1>(List<T1> T, bool IsCommit = true) where T1 : class
  369. {
  370. try
  371. {
  372. if (T == null || T.Count == ) return false;
  373. this.Context.Set<T1>().Local.Clear();
  374. foreach (var item in T)
  375. {
  376. this.Context.Set<T1>().Add(item);
  377. }
  378. if (IsCommit)
  379. {
  380. return this.Context.SaveChanges() > ;
  381. }
  382. return false;
  383. }
  384. catch (Exception e)
  385. {
  386. throw e;
  387. }
  388. }
  389. /// <summary>
  390. /// 增加多条记录,独立模型(异步方式)
  391. /// </summary>
  392. /// <param name="T1">实体模型集合</param>
  393. /// <param name="IsCommit">是否提交(默认提交)</param>
  394. /// <returns></returns>
  395. public virtual Task<bool> SaveListAsync<T1>(List<T1> T, bool IsCommit = true) where T1 : class
  396. {
  397. try
  398. {
  399. if (T == null || T.Count == ) return Task.FromResult<bool>(false);
  400. this.Context.Set<T1>().Local.Clear();
  401. foreach (var item in T)
  402. {
  403. this.Context.Set<T1>().Add(item);
  404. }
  405. if (IsCommit)
  406. {
  407. return Task.FromResult<bool>(this.Context.SaveChanges() > );
  408. }
  409. return Task.FromResult<bool>(false);
  410. }
  411. catch (Exception e)
  412. {
  413. throw e;
  414. }
  415. }
  416. /// <summary>
  417. /// 更新多模型,指定独立模型集合
  418. /// </summary>
  419. public virtual int UpdateList<T1>(List<T1> t) where T1 : class
  420. {
  421. if (t.Count <= ) return ;
  422. try
  423. {
  424. foreach (var item in t)
  425. {
  426. this.Context.Entry<T1>(item).State = System.Data.Entity.EntityState.Modified;
  427. }
  428. return this.Context.SaveChanges();
  429. }
  430. catch (Exception e)
  431. {
  432. throw e;
  433. }
  434. }
  435. /// <summary>
  436. /// 更新多模型,与当前模型一致
  437. /// </summary>
  438. public virtual int UpdateList(List<T> t)
  439. {
  440. if (t.Count <= ) return ;
  441. try
  442. {
  443. foreach (var item in t)
  444. {
  445. this.Context.Entry(item).State = System.Data.Entity.EntityState.Modified;
  446. }
  447. return this.Context.SaveChanges();
  448. }
  449. catch (Exception e) { throw e; }
  450. }
  451. /// <summary>
  452. /// 更新多条记录,同一模型
  453. /// </summary>
  454. /// <param name="t">实体模型集合</param>
  455. /// <param name="IsCommit">是否提交(默认提交)</param>
  456. /// <returns></returns>
  457. public virtual bool UpdateList(List<T> t, bool IsCommit = true)
  458. {
  459. if (t.Count <= ) return false;
  460. try
  461. {
  462. foreach (var item in t)
  463. {
  464. this.Context.Entry<T>(item).State = System.Data.Entity.EntityState.Modified;
  465. }
  466. if (IsCommit)
  467. {
  468. return this.Context.SaveChanges() > ;
  469. }
  470. return false;
  471. }
  472. catch (Exception e)
  473. {
  474. throw e;
  475. }
  476. }
  477. /// <summary>
  478. /// 更新多条记录,同一模型(异步方式)
  479. /// </summary>
  480. /// <param name="t">实体模型集合</param>
  481. /// <param name="IsCommit">是否提交(默认提交)</param>
  482. /// <returns></returns>
  483. public virtual Task<bool> UpdateListAsync(List<T> t, bool IsCommit = true)
  484. {
  485. if (t.Count <= ) return Task.FromResult<bool>(false);
  486. try
  487. {
  488. foreach (var item in t)
  489. {
  490. this.Context.Entry<T>(item).State = System.Data.Entity.EntityState.Modified;
  491. }
  492. if (IsCommit)
  493. {
  494. return Task.FromResult<bool>(this.Context.SaveChanges() > );
  495. }
  496. return Task.FromResult<bool>(false);
  497. }
  498. catch (Exception e)
  499. {
  500. throw e;
  501. }
  502. }
  503. /// <summary>
  504. /// 更新多条记录,独立模型
  505. /// </summary>
  506. /// <param name="t">实体模型集合</param>
  507. /// <param name="IsCommit">是否提交(默认提交)</param>
  508. /// <returns></returns>
  509. public virtual bool UpdateList<T1>(List<T1> t, bool IsCommit = true) where T1 : class
  510. {
  511. if (t.Count <= ) return false;
  512. try
  513. {
  514. foreach (var item in t)
  515. {
  516. this.Context.Entry<T1>(item).State = System.Data.Entity.EntityState.Modified;
  517. }
  518. if (IsCommit)
  519. {
  520. return this.Context.SaveChanges() > ;
  521. }
  522. return false;
  523. }
  524. catch (Exception e)
  525. {
  526. throw e;
  527. }
  528. }
  529. /// <summary>
  530. /// 更新多条记录,独立模型(异步方式)
  531. /// </summary>
  532. /// <param name="t">实体模型集合</param>
  533. /// <param name="IsCommit">是否提交(默认提交)</param>
  534. /// <returns></returns>
  535. public virtual Task<bool> UpdateListAsync<T1>(List<T1> t, bool IsCommit = true) where T1 : class
  536. {
  537. if (t.Count <= ) return Task.FromResult<bool>(false);
  538. try
  539. {
  540. foreach (var item in t)
  541. {
  542. this.Context.Entry<T1>(item).State = System.Data.Entity.EntityState.Modified;
  543. }
  544. if (IsCommit)
  545. {
  546. return Task.FromResult<bool>(this.Context.SaveChanges() > );
  547. }
  548. return Task.FromResult<bool>(false);
  549. }
  550. catch (Exception e)
  551. {
  552. throw e;
  553. }
  554. }
  555.  
  556. /// <summary>
  557. /// 批量删除数据,当前模型
  558. /// </summary>
  559. public virtual int DeleteList(List<T> t)
  560. {
  561. if (t == null || t.Count == ) return ;
  562. foreach (var item in t)
  563. {
  564. this.dbSet.Remove(item);
  565. }
  566. return this.Context.SaveChanges();
  567. }
  568. /// <summary>
  569. /// 批量删除数据,自定义模型
  570. /// </summary>
  571. public virtual int DeleteList<T1>(List<T1> t) where T1 : class
  572. {
  573. try
  574. {
  575. if (t == null || t.Count == ) return ;
  576. foreach (var item in t)
  577. {
  578. this.Context.Set<T1>().Remove(item);
  579. }
  580. return this.Context.SaveChanges();
  581. }
  582. catch (Exception e) { throw e; }
  583. }
  584.  
  585. /// <summary>
  586. /// 删除多条记录,同一模型
  587. /// </summary>
  588. /// <param name="T1">实体模型集合</param>
  589. /// <param name="IsCommit">是否提交(默认提交)</param>
  590. /// <returns></returns>
  591. public virtual bool DeleteList(List<T> T1, bool IsCommit = true)
  592. {
  593. if (T1 == null || T1.Count == ) return false;
  594. foreach (var item in T1)
  595. {
  596. this.dbSet.Remove(item);
  597. }
  598. if (IsCommit)
  599. {
  600. return this.Context.SaveChanges() > ;
  601. }
  602. return false;
  603. }
  604. /// <summary>
  605. /// 删除多条记录,同一模型(异步方式)
  606. /// </summary>
  607. /// <param name="T1">实体模型集合</param>
  608. /// <param name="IsCommit">是否提交(默认提交)</param>
  609. /// <returns></returns>
  610. public virtual Task<bool> DeleteListAsync(List<T> T1, bool IsCommit = true)
  611. {
  612. if (T1 == null || T1.Count == ) return Task.FromResult<bool>(false);
  613. foreach (var item in T1)
  614. {
  615. this.dbSet.Remove(item);
  616. }
  617. if (IsCommit)
  618. {
  619. return Task.FromResult<bool>(this.Context.SaveChanges() > );
  620. }
  621. return Task.FromResult<bool>(false);
  622. }
  623.  
  624. /// <summary>
  625. /// 删除多条记录,独立模型
  626. /// </summary>
  627. /// <param name="T1">实体模型集合</param>
  628. /// <param name="IsCommit">是否提交(默认提交)</param>
  629. /// <returns></returns>
  630. public virtual bool DeleteList<T1>(List<T1> t, bool IsCommit = true) where T1 : class
  631. {
  632. if (t == null || t.Count == ) return false;
  633. foreach (var item in t)
  634. {
  635. this.Context.Set<T1>().Remove(item);
  636. }
  637. if (IsCommit)
  638. {
  639. return this.Context.SaveChanges() > ;
  640. }
  641. return false;
  642. }
  643. /// <summary>
  644. /// 删除多条记录,独立模型(异步方式)
  645. /// </summary>
  646. /// <param name="T1">实体模型集合</param>
  647. /// <param name="IsCommit">是否提交(默认提交)</param>
  648. /// <returns></returns>
  649. public virtual Task<bool> DeleteListAsync<T1>(List<T1> T, bool IsCommit = true) where T1 : class
  650. {
  651. if (T == null || T.Count == ) return Task.FromResult<bool>(false);
  652. foreach (var item in T)
  653. {
  654. this.Context.Set<T1>().Remove(item);
  655. }
  656. if (IsCommit)
  657. {
  658. return Task.FromResult<bool>(this.Context.SaveChanges() > );
  659. }
  660. return Task.FromResult<bool>(false);
  661. }
  662.  
  663. /// <summary>
  664. /// 通过Lamda表达式,删除一条或多条记录
  665. /// </summary>
  666. /// <param name="predicate"></param>
  667. /// <param name="IsCommit"></param>
  668. /// <returns></returns>
  669. public virtual bool Delete(Expression<Func<T, bool>> predicate, bool IsCommit = true)
  670. {
  671. var data = this.dbSet.Where(predicate);
  672. foreach (var item in data)
  673. {
  674. this.dbSet.Remove(item);
  675. }
  676.  
  677. if (IsCommit)
  678. {
  679. return this.Context.SaveChanges() > ;
  680. }
  681. return false;
  682. }
  683. /// <summary>
  684. /// 通过Lamda表达式,删除一条或多条记录(异步方式)
  685. /// </summary>
  686. /// <param name="predicate"></param>
  687. /// <param name="IsCommit"></param>
  688. /// <returns></returns>
  689. public virtual Task<bool> DeleteAsync(Expression<Func<T, bool>> predicate, bool IsCommit = true)
  690. {
  691. var data = this.dbSet.Where(predicate);
  692. foreach (var item in data)
  693. {
  694. this.dbSet.Remove(item);
  695. }
  696.  
  697. if (IsCommit)
  698. {
  699. return Task.FromResult<bool>(this.Context.SaveChanges() > );
  700. }
  701. return Task.FromResult<bool>(false);
  702. }
  703.  
  704. #endregion
  705.  
  706. #region 固定公用帮助,含事务
  707.  
  708. private DbContext context = new MyConfig().db;
  709. /// <summary>
  710. /// 数据上下文--->根据Domain实体模型名称进行更改
  711. /// </summary>
  712. public DbContext Context
  713. {
  714. get
  715. {
  716. context.Configuration.ValidateOnSaveEnabled = false;
  717. return context;
  718. }
  719. }
  720. /// <summary>
  721. /// 数据上下文--->拓展属性
  722. /// </summary>
  723. public MyConfig Config
  724. {
  725. get
  726. {
  727. return new MyConfig();
  728. }
  729. }
  730. /// <summary>
  731. /// 公用泛型处理属性
  732. /// 注:所有泛型操作的基础
  733. /// </summary>
  734. public DbSet<T> dbSet
  735. {
  736. get { return this.Context.Set<T>(); }
  737. }
  738. /// <summary>
  739. /// 事务
  740. /// </summary>
  741. private DbContextTransaction _transaction = null;
  742. /// <summary>
  743. /// 开始事务
  744. /// </summary>
  745. public DbContextTransaction Transaction
  746. {
  747. get
  748. {
  749. if (this._transaction == null)
  750. {
  751. this._transaction = this.Context.Database.BeginTransaction();
  752. }
  753. return this._transaction;
  754. }
  755. set { this._transaction = value; }
  756. }
  757. /// <summary>
  758. /// 事务状态
  759. /// </summary>
  760. public bool Committed { get; set; }
  761. /// <summary>
  762. /// 异步锁定
  763. /// </summary>
  764. private readonly object sync = new object();
  765. /// <summary>
  766. /// 提交事务
  767. /// </summary>
  768. public void Commit()
  769. {
  770. if (!Committed)
  771. {
  772. lock (sync)
  773. {
  774. if (this._transaction != null)
  775. _transaction.Commit();
  776. }
  777. Committed = true;
  778. }
  779. }
  780. /// <summary>
  781. /// 回滚事务
  782. /// </summary>
  783. public void Rollback()
  784. {
  785. Committed = false;
  786. if (this._transaction != null)
  787. this._transaction.Rollback();
  788. }
  789. #endregion
  790.  
  791. #region 增删改操作
  792.  
  793. /// <summary>
  794. /// 添加一条模型记录,自动提交更改
  795. /// </summary>
  796. public virtual bool Save(T entity)
  797. {
  798. try
  799. {
  800. int row = ;
  801. var entry = this.Context.Entry<T>(entity);
  802. entry.State = System.Data.Entity.EntityState.Added;
  803. row = Context.SaveChanges();
  804. entry.State = System.Data.Entity.EntityState.Detached;
  805. return row > ;
  806. }
  807. catch (Exception e)
  808. {
  809. throw e;
  810. }
  811. }
  812.  
  813. /// <summary>
  814. /// 更新一条模型记录,自动提交更改
  815. /// </summary>
  816. public virtual bool Update(T entity)
  817. {
  818. try
  819. {
  820. int rows = ;
  821. var entry = this.Context.Entry(entity);
  822. entry.State = System.Data.Entity.EntityState.Modified;
  823. rows = this.Context.SaveChanges();
  824. entry.State = System.Data.Entity.EntityState.Detached;
  825. return rows > ;
  826. }
  827. catch (Exception e)
  828. {
  829. throw e;
  830. }
  831. }
  832.  
  833. /// <summary>
  834. /// 更新模型记录,如不存在进行添加操作
  835. /// </summary>
  836. public virtual bool SaveOrUpdate(T entity, bool isEdit)
  837. {
  838. try
  839. {
  840. return isEdit ? Update(entity) : Save(entity);
  841. }
  842. catch (Exception e) { throw e; }
  843. }
  844.  
  845. /// <summary>
  846. /// 删除一条或多条模型记录,含事务
  847. /// </summary>
  848. public virtual int Delete(Expression<Func<T, bool>> predicate = null)
  849. {
  850. try
  851. {
  852. int rows = ;
  853. IQueryable<T> entry = (predicate == null) ? this.dbSet.AsQueryable() : this.dbSet.Where(predicate);
  854. List<T> list = entry.ToList();
  855. if (list.Count > )
  856. {
  857. for (int i = ; i < list.Count; i++)
  858. {
  859. this.dbSet.Remove(list[i]);
  860. }
  861. rows = this.Context.SaveChanges();
  862. }
  863. return rows;
  864. }
  865. catch (Exception e)
  866. {
  867. throw e;
  868. }
  869. }
  870. /// <summary>
  871. /// 使用原始SQL语句,含事务处理
  872. /// </summary>
  873. public virtual int DeleteBySql(string sql, params DbParameter[] para)
  874. {
  875. try
  876. {
  877. return this.Context.Database.ExecuteSqlCommand(sql, para);
  878. }
  879. catch (Exception e)
  880. {
  881. throw e;
  882. }
  883. }
  884. /// <summary>
  885. /// 执行SQL删除(异步方式)
  886. /// </summary>
  887. /// <param name="sql">SQL语句</param>
  888. /// <param name="para">Parameters参数</param>
  889. public virtual Task<int> DeleteBySqlAsync(string sql, params DbParameter[] para)
  890. {
  891. return Task.FromResult<int>(DeleteBySql(sql, para));
  892. }
  893. #endregion
  894.  
  895. #region 存储过程操作
  896. /// <summary>
  897. /// 执行返回影响行数的存储过程
  898. /// </summary>
  899. /// <param name="procname">过程名称</param>
  900. /// <param name="parameter">参数对象</param>
  901. /// <returns></returns>
  902. public virtual object ExecuteProc(string procname, params DbParameter[] parameter)
  903. {
  904. try
  905. {
  906. return ExecuteSqlCommand(procname, parameter);
  907. }
  908. catch (Exception e)
  909. {
  910. throw e;
  911. }
  912. }
  913. /// <summary>
  914. /// 执行返回结果集的存储过程
  915. /// </summary>
  916. /// <param name="procname">过程名称</param>
  917. /// <param name="parameter">参数对象</param>
  918. /// <returns></returns>
  919. public virtual object ExecuteQueryProc(string procname, params DbParameter[] parameter)
  920. {
  921. try
  922. {
  923. return Context.Database.SqlFunctionForDynamic(procname, parameter);
  924. }
  925. catch (Exception e)
  926. {
  927. throw e;
  928. }
  929. }
  930. #endregion
  931.  
  932. #region 存在验证操作
  933. /// <summary>
  934. /// 验证当前条件是否存在相同项
  935. /// </summary>
  936. public virtual bool IsExist(Expression<Func<T, bool>> predicate)
  937. {
  938. var entry = this.dbSet.Where(predicate);
  939. return (entry.Any());
  940. }
  941.  
  942. /// <summary>
  943. /// 根据SQL验证实体对象是否存在
  944. /// </summary>
  945. public virtual bool IsExist(string sql, params DbParameter[] para)
  946. {
  947. IEnumerable result = this.Context.Database.SqlQuery(typeof(int), sql, para);
  948.  
  949. if (result.GetEnumerator().Current == null || result.GetEnumerator().Current.ToString() == "")
  950. return false;
  951. return true;
  952. }
  953.  
  954. /// <summary>
  955. /// 验证当前条件是否存在相同项(异步方式)
  956. /// </summary>
  957. public virtual Task<bool> IsExistAsync(Expression<Func<T, bool>> predicate)
  958. {
  959. return Task.FromResult<bool>(IsExist(predicate));
  960. }
  961.  
  962. /// <summary>
  963. /// 根据SQL验证实体对象是否存在(异步方式)
  964. /// </summary>
  965. public virtual Task<bool> IsExistAsync(string sql, params DbParameter[] para)
  966. {
  967. return Task.FromResult<bool>(IsExist(sql, para));
  968. }
  969. #endregion
  970.  
  971. #region 获取多条数据操作
  972. /// <summary>
  973. /// 返回IQueryable集合,延时加载数据
  974. /// </summary>
  975. public virtual IQueryable<T> LoadTop(Expression<Func<T, bool>> predicate, int TopNum)
  976. {
  977. try
  978. {
  979. if (predicate != null)
  980. {
  981. return this.dbSet.Where(predicate).Take(TopNum).AsNoTracking<T>();
  982. }
  983. return this.dbSet.AsQueryable<T>().AsNoTracking<T>();
  984. }
  985. catch (Exception e)
  986. {
  987. throw e;
  988. }
  989. }
  990. /// <summary>
  991. /// 返回IQueryable集合,延时加载数据
  992. /// </summary>
  993. public virtual IQueryable<T> LoadAll(Expression<Func<T, bool>> predicate)
  994. {
  995. try
  996. {
  997. if (predicate != null)
  998. {
  999. return this.dbSet.Where(predicate).AsNoTracking<T>();
  1000. }
  1001. return this.dbSet.AsQueryable<T>().AsNoTracking<T>();
  1002. }
  1003. catch (Exception e)
  1004. {
  1005. throw e;
  1006. }
  1007. }
  1008. /// <summary>
  1009. /// 返回IQueryable集合,延时加载数据(异步方式)
  1010. /// </summary>
  1011. /// <param name="predicate"></param>
  1012. /// <returns></returns>
  1013. public virtual Task<IQueryable<T>> LoadAllAsync(Expression<Func<T, bool>> predicate)
  1014. {
  1015. try
  1016. {
  1017. return Task.FromResult<IQueryable<T>>(LoadAll(predicate));
  1018. }
  1019. catch (Exception e)
  1020. {
  1021. throw e;
  1022. }
  1023. }
  1024.  
  1025. /// <summary>
  1026. /// 返回DbQuery集合,延时加载数据
  1027. /// </summary>
  1028. public virtual DbQuery<T> LoadQueryAll(Expression<Func<T, bool>> predicate)
  1029. {
  1030. try
  1031. {
  1032. if (predicate != null)
  1033. {
  1034. return this.dbSet.Where(predicate) as DbQuery<T>;
  1035. }
  1036. return this.dbSet;
  1037. }
  1038. catch (Exception e)
  1039. {
  1040. throw e;
  1041. }
  1042. }
  1043. /// <summary>
  1044. /// 获取DbQuery的列表(异步方式)
  1045. /// </summary>
  1046. /// <param name="predicate"></param>
  1047. /// <returns></returns>
  1048. public virtual Task<DbQuery<T>> LoadQueryAllAsync(Expression<Func<T, bool>> predicate)
  1049. {
  1050. try
  1051. {
  1052. return Task.FromResult<DbQuery<T>>(LoadQueryAll(predicate));
  1053. }
  1054. catch (Exception e)
  1055. {
  1056. throw e;
  1057. }
  1058. }
  1059. /// <summary>
  1060. /// 返回List集合,不采用延时加载
  1061. /// </summary>
  1062. public virtual List<T> LoadListAll(Expression<Func<T, bool>> predicate)
  1063. {
  1064. try
  1065. {
  1066. if (predicate != null)
  1067. {
  1068. return this.dbSet.Where(predicate).AsNoTracking().ToList();
  1069. }
  1070. return this.dbSet.AsQueryable<T>().AsNoTracking().ToList();
  1071. }
  1072. catch (Exception e)
  1073. {
  1074. throw e;
  1075. }
  1076. }
  1077. /// <summary>
  1078. /// 返回List<T>集合,不采用延时加载(异步方式)
  1079. /// </summary>
  1080. /// <param name="predicate"></param>
  1081. /// <returns></returns>
  1082. public virtual Task<List<T>> LoadListAllAsync(Expression<Func<T, bool>> predicate)
  1083. {
  1084. try
  1085. {
  1086. return Task.FromResult<List<T>>(LoadListAll(predicate));
  1087. }
  1088. catch (Exception e)
  1089. {
  1090. throw e;
  1091. }
  1092. }
  1093. /// <summary>
  1094. /// 返回IEnumerable集合,采用原始T-Sql方式
  1095. /// </summary>
  1096. public virtual IEnumerable<T> LoadEnumerableAll(string sql, params DbParameter[] para)
  1097. {
  1098. try
  1099. {
  1100. return this.Context.Database.SqlQuery<T>(sql, para);
  1101. }
  1102. catch (Exception e)
  1103. {
  1104. throw e;
  1105. }
  1106. }
  1107. /// <summary>
  1108. /// 获取IEnumerable列表(异步方式)
  1109. /// </summary>
  1110. /// <param name="sql">SQL语句</param>
  1111. /// <param name="para">Parameters参数</param>
  1112. /// <returns></returns>
  1113. public virtual Task<IEnumerable<T>> LoadEnumerableAllAsync(string sql, params DbParameter[] para)
  1114. {
  1115. try
  1116. {
  1117. return Task.FromResult<IEnumerable<T>>(LoadEnumerableAll(sql, para));
  1118. }
  1119. catch (Exception e)
  1120. {
  1121. throw e;
  1122. }
  1123. }
  1124. /// <summary>
  1125. /// 返回IEnumerable动态集合,采用原始T-Sql方式
  1126. /// </summary>
  1127. public virtual System.Collections.IEnumerable LoadEnumerable(string sql, params DbParameter[] para)
  1128. {
  1129. try
  1130. {
  1131. return this.Context.Database.SqlQueryForDynamic(sql, para);
  1132. }
  1133. catch (Exception e)
  1134. {
  1135. throw e;
  1136. }
  1137. }
  1138. /// <summary>
  1139. /// 获取数据动态集合(异步方式)
  1140. /// </summary>
  1141. /// <param name="sql">SQL语句</param>
  1142. /// <param name="para">Parameters参数</param>
  1143. /// <returns></returns>
  1144. public virtual Task<IEnumerable> LoadEnumerableAsync(string sql, params DbParameter[] para)
  1145. {
  1146. try
  1147. {
  1148. return Task.FromResult<IEnumerable>(LoadEnumerable(sql, para));
  1149. }
  1150. catch (Exception e)
  1151. {
  1152. throw e;
  1153. }
  1154. }
  1155. /// <summary>
  1156. /// 返回IList集合,采用原始T-Sql方式
  1157. /// </summary>
  1158. public virtual List<T> SelectBySql(string sql, params DbParameter[] para)
  1159. {
  1160. try
  1161. {
  1162. return this.Context.Database.SqlQuery(typeof(T), sql, para).Cast<T>().ToList();
  1163. }
  1164. catch (Exception e)
  1165. {
  1166. throw e;
  1167. }
  1168. }
  1169. /// <summary>
  1170. /// 采用SQL进行数据的查询,返回IList集合(异步方式)
  1171. /// </summary>
  1172. /// <param name="sql">SQL语句</param>
  1173. /// <param name="para">Parameters参数</param>
  1174. /// <returns></returns>
  1175. public virtual Task<List<T>> SelectBySqlAsync(string sql, params DbParameter[] para)
  1176. {
  1177. try
  1178. {
  1179. return Task.FromResult<List<T>>(SelectBySql<T>(sql, para));
  1180. }
  1181. catch (Exception e)
  1182. {
  1183. throw e;
  1184. }
  1185. }
  1186. /// <summary>
  1187. /// 采用SQL进行数据的查询,指定泛型,返回IList集合
  1188. /// </summary>
  1189. /// <typeparam name="T1"></typeparam>
  1190. /// <param name="sql"></param>
  1191. /// <param name="para"></param>
  1192. /// <returns></returns>
  1193. public virtual Task<List<T1>> SelectBySqlAsync<T1>(string sql, params DbParameter[] para)
  1194. {
  1195. try
  1196. {
  1197. return Task.FromResult<List<T1>>(SelectBySql<T1>(sql, para));
  1198. }
  1199. catch (Exception e)
  1200. {
  1201. throw e;
  1202. }
  1203. }
  1204.  
  1205. /// <summary>
  1206. /// 指定泛型,返回IList集合,采用原始T-Sql方式
  1207. /// </summary>
  1208. public virtual List<T1> SelectBySql<T1>(string sql, params DbParameter[] para)
  1209. {
  1210. try
  1211. {
  1212. return this.Context.Database.SqlQuery<T1>(sql, para).ToList();
  1213. }
  1214. catch (Exception e)
  1215. {
  1216. throw e;
  1217. }
  1218. }
  1219. /// <summary>
  1220. /// 可指定返回结果、排序、查询条件的通用查询方法,返回实体对象
  1221. /// </summary>
  1222. /// <typeparam name="TEntity">实体对象</typeparam>
  1223. /// <typeparam name="TOrderBy">排序字段类型</typeparam>
  1224. /// <typeparam name="TResult">数据结果,与TEntity一致</typeparam>
  1225. /// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
  1226. /// <param name="orderby">排序字段</param>
  1227. /// <param name="selector">返回结果(必须是模型中存在的字段)</param>
  1228. /// <param name="IsAsc">排序方向,true为正序false为倒序</param>
  1229. /// <returns>实体集合</returns>
  1230. public virtual List<TResult> QueryEntity<TEntity, TOrderBy, TResult>
  1231. (Expression<Func<TEntity, bool>> where,
  1232. Expression<Func<TEntity, TOrderBy>> orderby,
  1233. Expression<Func<TEntity, TResult>> selector,
  1234. bool IsAsc)
  1235. where TEntity : class
  1236. where TResult : class
  1237. {
  1238. IQueryable<TEntity> query = this.Context.Set<TEntity>();
  1239. if (where != null)
  1240. {
  1241. query = query.Where(where);
  1242. }
  1243.  
  1244. if (orderby != null)
  1245. {
  1246. query = IsAsc ? query.OrderBy(orderby) : query.OrderByDescending(orderby);
  1247. }
  1248. if (selector == null)
  1249. {
  1250. return query.Cast<TResult>().AsNoTracking().ToList();
  1251. }
  1252. return query.Select(selector).AsNoTracking().ToList();
  1253. }
  1254.  
  1255. /// <summary>
  1256. /// 可指定返回结果、排序、查询条件的通用查询方法,返回实体对象集合(异步方式)
  1257. /// </summary>
  1258. /// <typeparam name="TEntity">实体对象</typeparam>
  1259. /// <typeparam name="TOrderBy">排序字段类型</typeparam>
  1260. /// <typeparam name="TResult">数据结果,与TEntity一致</typeparam>
  1261. /// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
  1262. /// <param name="orderby">排序字段</param>
  1263. /// <param name="selector">返回结果(必须是模型中存在的字段)</param>
  1264. /// <param name="IsAsc">排序方向,true为正序false为倒序</param>
  1265. /// <returns>实体集合</returns>
  1266. public virtual Task<List<TResult>> QueryEntityAsync<TEntity, TOrderBy, TResult>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Expression<Func<TEntity, TResult>> selector, bool IsAsc)
  1267. where TEntity : class
  1268. where TResult : class
  1269. {
  1270. return Task.FromResult<List<TResult>>(QueryEntity(where, orderby, selector, IsAsc));
  1271. }
  1272.  
  1273. /// <summary>
  1274. /// 可指定返回结果、排序、查询条件的通用查询方法,返回Object对象
  1275. /// </summary>
  1276. /// <typeparam name="TEntity">实体对象</typeparam>
  1277. /// <typeparam name="TOrderBy">排序字段类型</typeparam>
  1278. /// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
  1279. /// <param name="orderby">排序字段</param>
  1280. /// <param name="selector">返回结果(必须是模型中存在的字段)</param>
  1281. /// <param name="IsAsc">排序方向,true为正序false为倒序</param>
  1282. /// <returns>自定义实体集合</returns>
  1283. public virtual List<object> QueryObject<TEntity, TOrderBy>
  1284. (Expression<Func<TEntity, bool>> where,
  1285. Expression<Func<TEntity, TOrderBy>> orderby,
  1286. Func<IQueryable<TEntity>,
  1287. List<object>> selector,
  1288. bool IsAsc)
  1289. where TEntity : class
  1290. {
  1291. IQueryable<TEntity> query = this.Context.Set<TEntity>();
  1292. if (where != null)
  1293. {
  1294. query = query.Where(where);
  1295. }
  1296.  
  1297. if (orderby != null)
  1298. {
  1299. query = IsAsc ? query.OrderBy(orderby) : query.OrderByDescending(orderby);
  1300. }
  1301. if (selector == null)
  1302. {
  1303. return query.AsNoTracking().ToList<object>();
  1304. }
  1305. return selector(query);
  1306. }
  1307.  
  1308. /// <summary>
  1309. /// 可指定返回结果、排序、查询条件的通用查询方法,返回Object对象集合(异步方式)
  1310. /// </summary>
  1311. /// <typeparam name="TEntity">实体对象</typeparam>
  1312. /// <typeparam name="TOrderBy">排序字段类型</typeparam>
  1313. /// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
  1314. /// <param name="orderby">排序字段</param>
  1315. /// <param name="selector">返回结果(必须是模型中存在的字段)</param>
  1316. /// <param name="IsAsc">排序方向,true为正序false为倒序</param>
  1317. /// <returns>自定义实体集合</returns>
  1318. public virtual Task<List<object>> QueryObjectAsync<TEntity, TOrderBy>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Func<IQueryable<TEntity>, List<object>> selector, bool IsAsc)
  1319. where TEntity : class
  1320. {
  1321. return Task.FromResult<List<object>>(QueryObject<TEntity, TOrderBy>(where, orderby, selector, IsAsc));
  1322. }
  1323.  
  1324. /// <summary>
  1325. /// 可指定返回结果、排序、查询条件的通用查询方法,返回动态类对象
  1326. /// </summary>
  1327. /// <typeparam name="TEntity">实体对象</typeparam>
  1328. /// <typeparam name="TOrderBy">排序字段类型</typeparam>
  1329. /// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
  1330. /// <param name="orderby">排序字段</param>
  1331. /// <param name="selector">返回结果(必须是模型中存在的字段)</param>
  1332. /// <param name="IsAsc">排序方向,true为正序false为倒序</param>
  1333. /// <returns>动态类</returns>
  1334. public virtual dynamic QueryDynamic<TEntity, TOrderBy>
  1335. (Expression<Func<TEntity, bool>> where,
  1336. Expression<Func<TEntity, TOrderBy>> orderby,
  1337. Func<IQueryable<TEntity>,
  1338. List<object>> selector,
  1339. bool IsAsc)
  1340. where TEntity : class
  1341. {
  1342. List<object> list = QueryObject<TEntity, TOrderBy>
  1343. (where, orderby, selector, IsAsc);
  1344. return Common.JsonConverter.JsonClass(list);
  1345. }
  1346. /// <summary>
  1347. /// 可指定返回结果、排序、查询条件的通用查询方法,返回动态类对象集合(异步方式)
  1348. /// </summary>
  1349. /// <typeparam name="TEntity">实体对象</typeparam>
  1350. /// <typeparam name="TOrderBy">排序字段类型</typeparam>
  1351. /// <param name="where">过滤条件,需要用到类型转换的需要提前处理与数据表一致的</param>
  1352. /// <param name="orderby">排序字段</param>
  1353. /// <param name="selector">返回结果(必须是模型中存在的字段)</param>
  1354. /// <param name="IsAsc">排序方向,true为正序false为倒序</param>
  1355. /// <returns>动态类</returns>
  1356. public virtual Task<dynamic> QueryDynamicAsync<TEntity, TOrderBy>(Expression<Func<TEntity, bool>> where, Expression<Func<TEntity, TOrderBy>> orderby, Func<IQueryable<TEntity>, List<object>> selector, bool IsAsc)
  1357. where TEntity : class
  1358. {
  1359. return Task.FromResult<dynamic>(QueryDynamic<TEntity, TOrderBy>(where, orderby, selector, IsAsc));
  1360. }
  1361. #endregion
  1362.  
  1363. #region 分页操作
  1364. /// <summary>
  1365. /// 待自定义分页函数,使用必须重写,指定数据模型
  1366. /// </summary>
  1367. public virtual IList<T1> PageByListSql<T1>(string sql, IList<DbParameter> parameters, Common.PageCollection page)
  1368. {
  1369. return null;
  1370. }
  1371. /// <summary>
  1372. /// 待自定义分页函数,使用必须重写,
  1373. /// </summary>
  1374. public virtual IList<T> PageByListSql(string sql, IList<DbParameter> parameters, Common.PageCollection page)
  1375. {
  1376. return null;
  1377. }
  1378.  
  1379. /// <summary>
  1380. /// 对IQueryable对象进行分页逻辑处理,过滤、查询项、排序对IQueryable操作
  1381. /// </summary>
  1382. /// <param name="t">Iqueryable</param>
  1383. /// <param name="index">当前页</param>
  1384. /// <param name="PageSize">每页显示多少条</param>
  1385. /// <returns>当前IQueryable to List的对象</returns>
  1386. public virtual Common.PageInfo<T> Query(IQueryable<T> query, int index, int PageSize)
  1387. {
  1388. if (index < )
  1389. {
  1390. index = ;
  1391. }
  1392. if (PageSize <= )
  1393. {
  1394. PageSize = ;
  1395. }
  1396. int count = query.Count();
  1397.  
  1398. int maxpage = count / PageSize;
  1399.  
  1400. if (count % PageSize > )
  1401. {
  1402. maxpage++;
  1403. }
  1404. if (index > maxpage)
  1405. {
  1406. index = maxpage;
  1407. }
  1408. if (count > )
  1409. query = query.Skip((index - ) * PageSize).Take(PageSize);
  1410. return new Common.PageInfo<T>(index, PageSize, count, query.ToList());
  1411. }
  1412.  
  1413. public virtual Common.PageInfo<object> FindAll<TEntity>(int PageIndex, int PageSize, Expression<Func<TEntity, bool>> condition, String orderByExpression, bool IsDESC)
  1414. where TEntity : class
  1415. {
  1416. var property = typeof(TEntity).GetProperty(orderByExpression);
  1417. var parameter = Expression.Parameter(typeof(TEntity), "p");
  1418. var propertyAccess = Expression.MakeMemberAccess(parameter, property);
  1419. var orderByExp = Expression.Lambda(propertyAccess, parameter);
  1420.  
  1421. IQueryable<TEntity> query = this.Context.Set<TEntity>();
  1422. if (condition != null)
  1423. {
  1424. query = query.Where(condition);
  1425. }
  1426. int count = query.Count();
  1427.  
  1428. string methodName = IsDESC ? "OrderByDescending" : "OrderBy";
  1429. MethodCallExpression resultExp = Expression.Call(typeof(Queryable), methodName, new Type[] { typeof(TEntity), property.PropertyType }, query.Expression, Expression.Quote(orderByExp));
  1430. query = query.Provider.CreateQuery<TEntity>(resultExp);
  1431.  
  1432. return new Common.PageInfo<object>(PageIndex, PageSize, count, query.ToList<object>());
  1433. }
  1434.  
  1435. /// <summary>
  1436. /// 通用EF分页,默认显示20条记录
  1437. /// </summary>
  1438. /// <typeparam name="TEntity">实体模型</typeparam>
  1439. /// <typeparam name="TOrderBy">排序类型</typeparam>
  1440. /// <param name="index">当前页</param>
  1441. /// <param name="pageSize">显示条数</param>
  1442. /// <param name="where">过滤条件</param>
  1443. /// <param name="orderby">排序字段</param>
  1444. /// <param name="selector">结果集合</param>
  1445. /// <param name="isAsc">排序方向true正序 false倒序</param>
  1446. /// <returns>自定义实体集合</returns>
  1447. public virtual Common.PageInfo<object> Query<TEntity, TOrderBy>
  1448. (int index, int pageSize,
  1449. Expression<Func<TEntity, bool>> where,
  1450. Expression<Func<TEntity, TOrderBy>> orderby,
  1451. Func<IQueryable<TEntity>, List<object>> selector,
  1452. bool isAsc)
  1453. where TEntity : class
  1454. {
  1455. if (index < )
  1456. {
  1457. index = ;
  1458. }
  1459.  
  1460. if (pageSize <= )
  1461. {
  1462. pageSize = ;
  1463. }
  1464.  
  1465. IQueryable<TEntity> query = this.Context.Set<TEntity>();
  1466. if (where != null)
  1467. {
  1468. query = query.Where(where);
  1469. }
  1470. int count = query.Count();
  1471.  
  1472. int maxpage = count / pageSize;
  1473.  
  1474. if (count % pageSize > )
  1475. {
  1476. maxpage++;
  1477. }
  1478. if (index > maxpage)
  1479. {
  1480. index = maxpage;
  1481. }
  1482.  
  1483. if (orderby != null)
  1484. {
  1485. query = isAsc ? query.OrderBy(orderby) : query.OrderByDescending(orderby);
  1486. }
  1487. if (count > )
  1488. query = query.Skip((index - ) * pageSize).Take(pageSize);
  1489. //返回结果为null,返回所有字段
  1490. if (selector == null)
  1491. return new Common.PageInfo<object>(index, pageSize, count, query.ToList<object>());
  1492. return new Common.PageInfo<object>(index, pageSize, count, selector(query).ToList());
  1493. }
  1494. /// <summary>
  1495. /// 普通SQL查询分页方法
  1496. /// </summary>
  1497. /// <param name="index">当前页</param>
  1498. /// <param name="pageSize">显示行数</param>
  1499. /// <param name="tableName">表名/视图</param>
  1500. /// <param name="field">获取项</param>
  1501. /// <param name="filter">过滤条件</param>
  1502. /// <param name="orderby">排序字段+排序方向</param>
  1503. /// <param name="group">分组字段</param>
  1504. /// <returns>结果集</returns>
  1505. public virtual Common.PageInfo Query(int index, int pageSize, string tableName, string field, string filter, string orderby, string group, params DbParameter[] para)
  1506. {
  1507. //执行分页算法
  1508. if (index <= )
  1509. index = ;
  1510. int start = (index - ) * pageSize;
  1511. if (start > )
  1512. start -= ;
  1513. else
  1514. start = ;
  1515. int end = index * pageSize;
  1516.  
  1517. #region 查询逻辑
  1518. string logicSql = "SELECT";
  1519. //查询项
  1520. if (!string.IsNullOrEmpty(field))
  1521. {
  1522. logicSql += " " + field;
  1523. }
  1524. else
  1525. {
  1526. logicSql += " *";
  1527. }
  1528. logicSql += " FROM (" + tableName + " ) where";
  1529. //过滤条件
  1530. if (!string.IsNullOrEmpty(filter))
  1531. {
  1532. logicSql += " " + filter;
  1533. }
  1534. else
  1535. {
  1536. filter = " 1=1";
  1537. logicSql += " 1=1";
  1538. }
  1539. //分组
  1540. if (!string.IsNullOrEmpty(group))
  1541. {
  1542. logicSql += " group by " + group;
  1543. }
  1544.  
  1545. #endregion
  1546.  
  1547. //获取当前条件下数据总条数
  1548. int count = this.Context.Database.SqlQuery(typeof(int), "select count(*) from (" + tableName + ") where " + filter, para).Cast<int>().FirstOrDefault();
  1549. string sql = "SELECT T.* FROM ( SELECT B.* FROM ( SELECT A.*,ROW_NUMBER() OVER(ORDER BY getdate()) as RN" +
  1550. logicSql + ") A ) B WHERE B.RN<=" + end + ") T WHERE T.RN>" + start;
  1551. //排序
  1552. if (!string.IsNullOrEmpty(orderby))
  1553. {
  1554. sql += " order by " + orderby;
  1555. }
  1556. var list = ExecuteSqlQuery(sql, para) as IEnumerable;
  1557. if (list != null)
  1558. return new Common.PageInfo(index, pageSize, count, list.Cast<object>().ToList());
  1559. return new Common.PageInfo(index, pageSize, count, new { });
  1560. }
  1561.  
  1562. /// <summary>
  1563. /// 最简单的SQL分页
  1564. /// </summary>
  1565. /// <param name="index">页码</param>
  1566. /// <param name="pageSize">显示行数</param>
  1567. /// <param name="sql">纯SQL语句</param>
  1568. /// <param name="orderby">排序字段与方向</param>
  1569. /// <returns></returns>
  1570. public virtual Common.PageInfo Query(int index, int pageSize, string sql, string orderby, params DbParameter[] para)
  1571. {
  1572. return this.Query(index, pageSize, sql, null, null, orderby, null, para);
  1573. }
  1574. /// <summary>
  1575. /// 多表联合分页算法
  1576. /// </summary>
  1577. public virtual Common.PageInfo Query(IQueryable query, int index, int PageSize)
  1578. {
  1579. var enumerable = (query as System.Collections.IEnumerable).Cast<object>();
  1580. if (index < )
  1581. {
  1582. index = ;
  1583. }
  1584. if (PageSize <= )
  1585. {
  1586. PageSize = ;
  1587. }
  1588.  
  1589. int count = enumerable.Count();
  1590.  
  1591. int maxpage = count / PageSize;
  1592.  
  1593. if (count % PageSize > )
  1594. {
  1595. maxpage++;
  1596. }
  1597. if (index > maxpage)
  1598. {
  1599. index = maxpage;
  1600. }
  1601. if (count > )
  1602. enumerable = enumerable.Skip((index - ) * PageSize).Take(PageSize);
  1603. return new Common.PageInfo(index, PageSize, count, enumerable.ToList());
  1604. }
  1605. #endregion
  1606.  
  1607. #region ADO.NET增删改查方法
  1608. /// <summary>
  1609. /// 执行增删改方法,含事务处理
  1610. /// </summary>
  1611. public virtual object ExecuteSqlCommand(string sql, params DbParameter[] para)
  1612. {
  1613. try
  1614. {
  1615. return this.Context.Database.ExecuteSqlCommand(sql, para);
  1616. }
  1617. catch (Exception e)
  1618. {
  1619. throw e;
  1620. }
  1621.  
  1622. }
  1623. /// <summary>
  1624. /// 执行多条SQL,增删改方法,含事务处理
  1625. /// </summary>
  1626. public virtual object ExecuteSqlCommand(Dictionary<string, object> sqllist)
  1627. {
  1628. try
  1629. {
  1630. int rows = ;
  1631. IEnumerator<KeyValuePair<string, object>> enumerator = sqllist.GetEnumerator();
  1632. using (Transaction)
  1633. {
  1634. while (enumerator.MoveNext())
  1635. {
  1636. rows += this.Context.Database.ExecuteSqlCommand(enumerator.Current.Key, enumerator.Current.Value);
  1637. }
  1638. Commit();
  1639. }
  1640. return rows;
  1641. }
  1642. catch (Exception e)
  1643. {
  1644. Rollback();
  1645. throw e;
  1646. }
  1647.  
  1648. }
  1649. /// <summary>
  1650. /// 执行查询方法,返回动态类,接收使用var,遍历时使用dynamic类型
  1651. /// </summary>
  1652. public virtual object ExecuteSqlQuery(string sql, params DbParameter[] para)
  1653. {
  1654. try
  1655. {
  1656. return this.Context.Database.SqlQueryForDynamic(sql, para);
  1657. }
  1658. catch (Exception e)
  1659. {
  1660. throw e;
  1661. }
  1662. }
  1663. #endregion
  1664. }
  1665. }

context:

  1. namespace Service
  2. {
  3. public interface IUnitOfWork
  4. {
  5. /// <summary>
  6. /// 提交
  7. /// </summary>
  8. /// <returns></returns>
  9. bool Commit();
  10. }
  11. }
  1. using Domain;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data.Entity;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8.  
  9. namespace Service
  10. {
  11. public class UnitOfWork : IUnitOfWork, IDisposable
  12. {
  13. #region 数据上下文
  14.  
  15. private DbContext context = new MyConfig().db;
  16. /// <summary>
  17. /// 数据上下文
  18. /// </summary>
  19. public DbContext _Context
  20. {
  21. get
  22. {
  23. context.Configuration.ValidateOnSaveEnabled = false;
  24. return context;
  25. }
  26. }
  27.  
  28. #endregion
  29.  
  30. /// <summary>
  31. /// 提交
  32. /// </summary>
  33. /// <returns></returns>
  34. public bool Commit()
  35. {
  36. return _Context.SaveChanges() > ;
  37. }
  38.  
  39. public void Dispose()
  40. {
  41. if (_Context != null)
  42. {
  43. _Context.Dispose();
  44. }
  45. GC.SuppressFinalize(this);
  46. }
  47. }
  48. }

扩展:

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Data;
  6. using System.Data.Entity;
  7. using System.Linq;
  8. using System.Reflection;
  9. using System.Reflection.Emit;
  10. using System.Text;
  11.  
  12. namespace Service
  13. {
  14. /// <summary>
  15. /// 查询动态类
  16. /// add yuangang by 2016-05-10
  17. /// </summary>
  18. public static class DatabaseExtensions
  19. {
  20. /// <summary>
  21. /// 自定义Connection对象
  22. /// </summary>
  23. private static IDbConnection DefaultConnection
  24. {
  25. get
  26. {
  27. return Domain.MyConfig.DefaultConnection;
  28. }
  29. }
  30. /// <summary>
  31. /// 自定义数据库连接字符串,与EF连接模式一致
  32. /// </summary>
  33. private static string DefaultConnectionString
  34. {
  35. get
  36. {
  37. return Domain.MyConfig.DefaultConnectionString;
  38. }
  39. }
  40. /// <summary>
  41. /// 动态查询主方法
  42. /// </summary>
  43. /// <returns></returns>
  44. public static IEnumerable SqlQueryForDynamic(this Database db, string sql, params object[] parameters)
  45. {
  46. IDbConnection defaultConn = DefaultConnection;
  47.  
  48. //ADO.NET数据库连接字符串
  49. db.Connection.ConnectionString = DefaultConnectionString;
  50.  
  51. return SqlQueryForDynamicOtherDB(db, sql, defaultConn, parameters);
  52. }
  53. private static IEnumerable SqlQueryForDynamicOtherDB(this Database db, string sql, IDbConnection conn, params object[] parameters)
  54. {
  55. conn.ConnectionString = db.Connection.ConnectionString;
  56.  
  57. if (conn.State != ConnectionState.Open)
  58. {
  59. conn.Open();
  60. }
  61.  
  62. IDbCommand cmd = conn.CreateCommand();
  63. cmd.CommandText = sql;
  64. if (parameters != null)
  65. {
  66. foreach (var item in parameters)
  67. {
  68. cmd.Parameters.Add(item);
  69. }
  70. }
  71.  
  72. using (IDataReader dataReader = cmd.ExecuteReader())
  73. {
  74.  
  75. if (!dataReader.Read())
  76. {
  77. return null; //无结果返回Null
  78. }
  79.  
  80. #region 构建动态字段
  81.  
  82. TypeBuilder builder = DatabaseExtensions.CreateTypeBuilder(
  83. "EF_DynamicModelAssembly",
  84. "DynamicModule",
  85. "DynamicType");
  86.  
  87. int fieldCount = dataReader.FieldCount;
  88. for (int i = ; i < fieldCount; i++)
  89. {
  90. Type t = dataReader.GetFieldType(i);
  91. switch (t.Name.ToLower())
  92. {
  93. case "decimal":
  94. t = typeof(Decimal?);
  95. break;
  96. case "double":
  97. t = typeof(Double?);
  98. break;
  99. case "datetime":
  100. t = typeof(DateTime?);
  101. break;
  102. case "single":
  103. t = typeof(float?);
  104. break;
  105. case "int16":
  106. t = typeof(int?);
  107. break;
  108. case "int32":
  109. t = typeof(int?);
  110. break;
  111. case "int64":
  112. t = typeof(int?);
  113. break;
  114. default:
  115. break;
  116. }
  117. DatabaseExtensions.CreateAutoImplementedProperty(
  118. builder,
  119. dataReader.GetName(i),
  120. t);
  121. }
  122.  
  123. #endregion
  124.  
  125. cmd.Parameters.Clear();
  126. dataReader.Close();
  127. dataReader.Dispose();
  128. cmd.Dispose();
  129. conn.Close();
  130. conn.Dispose();
  131.  
  132. Type returnType = builder.CreateType();
  133.  
  134. if (parameters != null)
  135. {
  136. return db.SqlQuery(returnType, sql, parameters);
  137. }
  138. else
  139. {
  140. return db.SqlQuery(returnType, sql);
  141. }
  142. }
  143. }
  144.  
  145. private static TypeBuilder CreateTypeBuilder(string assemblyName, string moduleName, string typeName)
  146. {
  147. TypeBuilder typeBuilder = AppDomain.CurrentDomain.DefineDynamicAssembly(
  148. new AssemblyName(assemblyName),
  149. AssemblyBuilderAccess.Run).DefineDynamicModule(moduleName).DefineType(typeName,
  150. TypeAttributes.Public);
  151. typeBuilder.DefineDefaultConstructor(MethodAttributes.Public);
  152. return typeBuilder;
  153. }
  154.  
  155. private static void CreateAutoImplementedProperty(TypeBuilder builder, string propertyName, Type propertyType)
  156. {
  157. const string PrivateFieldPrefix = "m_";
  158. const string GetterPrefix = "get_";
  159. const string SetterPrefix = "set_";
  160.  
  161. // Generate the field.
  162. FieldBuilder fieldBuilder = builder.DefineField(
  163. string.Concat(
  164. PrivateFieldPrefix, propertyName),
  165. propertyType,
  166. FieldAttributes.Private);
  167.  
  168. // Generate the property
  169. PropertyBuilder propertyBuilder = builder.DefineProperty(
  170. propertyName,
  171. System.Reflection.PropertyAttributes.HasDefault,
  172. propertyType, null);
  173.  
  174. // Property getter and setter attributes.
  175. MethodAttributes propertyMethodAttributes = MethodAttributes.Public
  176. | MethodAttributes.SpecialName
  177. | MethodAttributes.HideBySig;
  178.  
  179. // Define the getter method.
  180. MethodBuilder getterMethod = builder.DefineMethod(
  181. string.Concat(
  182. GetterPrefix, propertyName),
  183. propertyMethodAttributes,
  184. propertyType,
  185. Type.EmptyTypes);
  186.  
  187. // Emit the IL code.
  188. // ldarg.0
  189. // ldfld,_field
  190. // ret
  191. ILGenerator getterILCode = getterMethod.GetILGenerator();
  192. getterILCode.Emit(OpCodes.Ldarg_0);
  193. getterILCode.Emit(OpCodes.Ldfld, fieldBuilder);
  194. getterILCode.Emit(OpCodes.Ret);
  195.  
  196. // Define the setter method.
  197. MethodBuilder setterMethod = builder.DefineMethod(
  198. string.Concat(SetterPrefix, propertyName),
  199. propertyMethodAttributes,
  200. null,
  201. new Type[] { propertyType });
  202.  
  203. // Emit the IL code.
  204. // ldarg.0
  205. // ldarg.1
  206. // stfld,_field
  207. // ret
  208. ILGenerator setterILCode = setterMethod.GetILGenerator();
  209. setterILCode.Emit(OpCodes.Ldarg_0);
  210. setterILCode.Emit(OpCodes.Ldarg_1);
  211. setterILCode.Emit(OpCodes.Stfld, fieldBuilder);
  212. setterILCode.Emit(OpCodes.Ret);
  213.  
  214. propertyBuilder.SetGetMethod(getterMethod);
  215. propertyBuilder.SetSetMethod(setterMethod);
  216. }
  217.  
  218. public static dynamic SqlFunctionForDynamic(this Database db, string sql, params object[] parameters)
  219. {
  220. IDbConnection conn = DefaultConnection;
  221.  
  222. //ADO.NET数据库连接字符串
  223. conn.ConnectionString = DefaultConnectionString;
  224.  
  225. if (conn.State != ConnectionState.Open)
  226. {
  227. conn.Open();
  228. }
  229.  
  230. IDbCommand cmd = conn.CreateCommand();
  231. cmd.CommandText = sql;
  232. cmd.CommandType = CommandType.StoredProcedure;
  233. if (parameters != null)
  234. {
  235. foreach (var item in parameters)
  236. {
  237. cmd.Parameters.Add(item);
  238. }
  239. }
  240. //1、DataReader查询数据
  241. using (IDataReader dataReader = cmd.ExecuteReader())
  242. {
  243. if (!dataReader.Read())
  244. {
  245. return null;
  246. }
  247. //2、DataReader转换Json
  248. string jsonstr = Common.JsonConverter.ToJson(dataReader);
  249. dataReader.Close();
  250. dataReader.Dispose();
  251. cmd.Dispose();
  252. conn.Close();
  253. conn.Dispose();
  254. //3、Json转换动态类
  255. dynamic dyna = Common.JsonConverter.ConvertJson(jsonstr);
  256. return dyna;
  257. }
  258. }
  259. /// <summary>
  260. /// 对可空类型进行判断转换(*要不然会报错)
  261. /// </summary>
  262. /// <param name="value">DataReader字段的值</param>
  263. /// <param name="conversionType">该字段的类型</param>
  264. /// <returns></returns>
  265. private static object CheckType(object value, Type conversionType)
  266. {
  267. if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
  268. {
  269. if (value == null)
  270. return null;
  271. System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(conversionType);
  272. conversionType = nullableConverter.UnderlyingType;
  273. }
  274. return Convert.ChangeType(value, conversionType);
  275. }
  276.  
  277. /// <summary>
  278. /// 判断指定对象是否是有效值
  279. /// </summary>
  280. /// <param name="obj"></param>
  281. /// <returns></returns>
  282. private static bool IsNullOrDBNull(object obj)
  283. {
  284. return (obj == null || (obj is DBNull)) ? true : false;
  285. }
  286. }
  287. }

在RepositoryBase中又增加了两个备用方法:

接口:

  1. #region 执行sql语句返回datatable
  2. object SqlQueryForScalar(string sql, DbParameter[] parameters);
  3. DataTable SqlQueryForDataTatable(string sql, DbParameter[] parameters);
  4. #endregion

实现:

  1. #region 执行sql语句返回datatable/object
  2. /// <summary>
  3. /// 执行查询并返回首行首列的值
  4. /// </summary>
  5. /// <param name="sql"></param>
  6. /// <param name="parameters"></param>
  7. /// <returns></returns>
  8. public object SqlQueryForScalar(string sql, DbParameter[] parameters)
  9. {
  10. string connStr = this.Context.Database.Connection.ConnectionString;
  11. using (SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr))
  12. {
  13. if (conn.State != ConnectionState.Open)
  14. {
  15. conn.Open();
  16. }
  17.  
  18. SqlCommand cmd = new SqlCommand();
  19. cmd.Connection = conn;
  20. cmd.CommandText = sql;
  21. cmd.CommandType = CommandType.Text;
  22.  
  23. if (parameters != null && parameters.Length > )
  24. {
  25. foreach (var item in parameters)
  26. {
  27. cmd.Parameters.Add(item);
  28. }
  29. }
  30. object obj = cmd.ExecuteScalar();
  31.  
  32. conn.Close();//连接需要关闭
  33. conn.Dispose();
  34. return obj;
  35. }
  36. }
  37. /// <summary>
  38. /// 执行查询并返回DataTable
  39. /// </summary>
  40. /// <param name="sql"></param>
  41. /// <param name="parameters"></param>
  42. /// <returns></returns>
  43. public DataTable SqlQueryForDataTatable(string sql, DbParameter[] parameters)
  44. {
  45. string connStr = this.Context.Database.Connection.ConnectionString;
  46. using (SqlConnection conn = new System.Data.SqlClient.SqlConnection(connStr))
  47. {
  48. if (conn.State != ConnectionState.Open)
  49. {
  50. conn.Open();
  51. }
  52. SqlCommand cmd = new SqlCommand();
  53. cmd.Connection = conn;
  54. cmd.CommandText = sql;
  55. cmd.CommandType = CommandType.Text;
  56.  
  57. if (parameters != null && parameters.Length > )
  58. {
  59. foreach (var item in parameters)
  60. {
  61. cmd.Parameters.Add(item);
  62. }
  63. }
  64.  
  65. SqlDataAdapter adapter = new SqlDataAdapter(cmd);
  66. DataTable table = new DataTable();
  67. adapter.Fill(table);
  68. return table;
  69. }
  70. }
  71. #endregion

MVC仓储类Repository的更多相关文章

  1. (译文)MVC通用仓储类

    Generic Repository Pattern MVC Generic Repository Pattern MVC 原文链接:http://www.codeproject.com/Articl ...

  2. MVC通用仓储类

    原文链接:http://www.codeproject.com/Articles/1095323/Generic-Repository-Pattern-MVC 良好的架构师任何项目的核心,开发人员一直 ...

  3. 在Apworks数据服务中使用基于Entity Framework Core的仓储(Repository)实现

    <在ASP.NET Core中使用Apworks快速开发数据服务>一文中,我介绍了如何使用Apworks框架的数据服务来快速构建用于查询和管理数据模型的RESTful API,通过该文的介 ...

  4. C#数据仓储类

    https://ninesky.codeplex.com/SourceControl/latest /*============================== 版本:v0.1 创建:2016.2 ...

  5. 用MVC5+EF6+WebApi 做一个考试功能(六) 仓储模式 打造EF通用仓储类

    前言 年底工作比较忙,年度总结还没写,项目要上线,回老家过年各种准备.尤其是给长辈给侄子侄女准备礼物头都大了. 原来想年前先出一版能用的,我看有点悬了,尽量先把大体功能弄出来,扔掉一些,保证能考试,然 ...

  6. MVC单元测试,使用Repository模式、Ninject、Moq

    本篇使用Repository设计MVC项目,使用Ninject作为DI容器,借助Moq进行单元测试. 模型和EF上下文 模型很简单: public class Foo { public int Id ...

  7. .Net Core之仓储(Repository)模式

    我们经常在项目中使用仓储(Repository)模式,来实现解耦数据访问层与业务层.那在.net core使用EF core又是怎么做的呢? 现在我分享一下我的实现方案: 一.在领域层创建Reposi ...

  8. (转)MVC中的Repository模式

    1.首先创建一个空的MVC3应用程序,命名为MyRepository.Web,解决方案命名为MyRepository. 2.添加一个类库项目,命名为MyRepository.DAL,添加一个文件夹命名 ...

  9. 扩展ASP.NET MVC HtmlHelper类

    在这篇帖子中我会使用一个示例演示扩展ASP.NET MVC HtmlHelper类,让它们可以在你的MVC视图中工作.这个示例中我会提供一个简单的方案生成Html表格. HtmlHelper类 Htm ...

随机推荐

  1. 第2课 C 到 C++ 的升级

    1.  C与C++的关系 (1)C++继承了所有的C特性,并在C的基础上提供了更多的语法和特性. (2)C++的设计目标是运行效率与开发效率的统一,它更强调的是语言的实用性. 2. C到C++ 的升级 ...

  2. 开启saltstack的web界面

    saltstack官方有提供一个web界面叫halite,halite是用cherrypy web框架开发的,连接后端的saltstack api,web界面虽然简单点,但功能还算齐全,今天就来开启s ...

  3. centos7配置yum源

    https://www.cnblogs.com/renpingsheng/p/7845096.html

  4. javascript 浮点数加减乘除计算会有问题, 整理了以下代码来规避这个问题

    /* * js数学计算 add by yan */ /** ** 加法函数,用来得到精确的加法结果 ** 说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显.这个函数返回较 ...

  5. Dictionary 字典的使用

    Dim a, d, i             '创建几个变量Set d = CreateObject("Scripting.Dictionary")d.Add "a&q ...

  6. Mysql 触发器 A表记录到B表

    1:查询出需要的列名 备用 #列名 select COLUMN_NAME from information_schema.columns where TABLE_SCHEMA='yunpiaobox_ ...

  7. WDA-BOPF业务对象处理框架

    SAP中的BOPF(Business Object Processing Framework) 正文 希望简化你的业务应用开发过程?业务对象处理框架(Business Object Processin ...

  8. vue 项目部署后 刷新一下 页面找不到 解决

    1.修改配置router的参数  (效果不好) 2. (不能解决 出现403) 后端配置例子 Apache <IfModule mod_rewrite.c> RewriteEngine O ...

  9. cocos2d-x 3.0 学习笔记: 一个可以拖拽的Label及schedule的应用

    #ifndef _DRAGLABEL_H_ #define _DRAGLABEL_H_ #include "cocos2d.h" USING_NS_CC; class DragLa ...

  10. one by one 项目 part 6

    package Controllerservlet; import java.io.IOException; import java.io.PrintWriter; import java.util. ...