- 1 using System;
- 2 using System.Collections.Generic;
- 3 using System.Linq;
- 4 using System.Text;
- 5 using System.Transactions;
- 6 using System.Data.Entity;
- 7 using System.Linq.Expressions;
- 8 using System.Data;
- 9 using System.Linq.Dynamic;
- 10 using EntityFramework.Extensions;
- 11 using System.Reflection;
- 12 using System.Data.Entity.Infrastructure;
- 13 using MySql.Data.MySqlClient;
- 14 /************************************************
- 15 ◇作者: LowKeyC 需要引用这个程序集:EntityFramework.Extended.6.1.0.168
- 16 ◇说明: 实现EF通用的CRUD通用的接口
- 17 ◇版本号:V1.0
- 18 ◇创建日期:2017年6月22日 星期四
- 19 *****************************************************/
- 20 namespace EFCommon.SqlHelp
- 21 {
- 22 public class Repository : IRepository, IDisposable
- 23 {
- 24
- 25 private readonly static
- DbContext _DbContextHandle =
- new
- ahavadbEntities();
- //此处进行调用EF的DBContent 的实体类或者通过工厂化模式来进行调用。
- 26
- 27 /// <summary>
- 28 /// 添加一个对象
- 29 /// </summary>
- 30 /// <typeparam name="T"></typeparam>
- 31 /// <param name="Entity"></param>
- 32 /// <returns></returns>
- 33 public bool
- Add(T Entity)
- where
- T :
- class
- 34 {
- 35 using
- (TransactionScope Ts =
- new TransactionScope(TransactionScopeOption.Required))
- 36 {
- 37
- _DbContextHandle.Set
- ().Add(Entity);
- 38 int
- Count =
- _DbContextHandle.SaveChanges();
- 39 Ts.Complete();
- 40 return
- Count >
- 0;
- 41 }
- 42 }
- 43
- 44 /// <summary>
- 45 /// 批量的插入数据
- 46 /// </summary>
- 47 /// <typeparam name="T"></typeparam>
- 48 /// <param name="Entity"></param>
- 49 /// <returns></returns>
- 50 public bool
- AddRange(List Entity)
- where
- T :
- class
- 51 {
- 52 using
- (TransactionScope Ts =
- new TransactionScope(TransactionScopeOption.Required))
- 53 {
- 54
- _DbContextHandle.Set
- ().AddRange(Entity);
- 55 int
- Count =
- _DbContextHandle.SaveChanges();
- 56 Ts.Complete();
- 57 return
- Count >
- 0;
- 58 }
- 59 }
- 60
- 61 /// <summary>
- 62 /// 根据查询条件进行删除对象
- 63 /// </summary>
- 64 /// <typeparam name="T"></typeparam>
- 65 /// <param name="whereLambda">查询条件</param>
- 66 /// <returns></returns>
- 67 public bool
- Delete(Expression
- bool
- >> whereLambda)
- where
- T :
- class
- 68 {
- 69 using
- (TransactionScope Ts =
- new TransactionScope(TransactionScopeOption.Required))
- 70 {
- 71 var
- EntityModel = _DbContextHandle.Set
- ().Where(whereLambda).FirstOrDefault();
- 72 if
- (EntityModel !=
- null)
- 73 {
- 74
- _DbContextHandle.Set
- ().Remove(EntityModel);
- 75 int
- Count =
- _DbContextHandle.SaveChanges();
- 76 Ts.Complete();
- 77 return
- Count >
- 0;
- 78 }
- 79 return false;
- 80 }
- 81 }
- 82
- 83
- 84 /// <summary>
- 85 /// 删除单个对象的实体
- 86 /// </summary>
- 87 /// <typeparam name="T"></typeparam>
- 88 /// <param name="Entity">实体对象</param>
- 89 /// <returns></returns>
- 90 public bool
- Delete(T Entity)
- where
- T :
- class
- 91 {
- 92 using
- (TransactionScope Ts =
- new TransactionScope(TransactionScopeOption.Required))
- 93 {
- 94
- _DbContextHandle.Set
- ().Attach(Entity);
- 95
- _DbContextHandle.Set
- ().Remove(Entity);
- 96 int
- Count =
- _DbContextHandle.SaveChanges();
- 97 Ts.Complete();
- 98 return
- Count >
- 0;
- 99 }
- 100 }
- 101
- 102 /// <summary>
- 103 /// 批量的进行更新数据
- 104 /// </summary>
- 105 /// <typeparam name="T"></typeparam>
- 106 /// <param name="Entity"></param>
- 107 /// <returns></returns>
- 108 public bool
- Update(List Entity)
- where
- T :
- class
- 109 {
- 110 int
- Count =
- 0;
- 111 using
- (TransactionScope Ts =
- new TransactionScope(TransactionScopeOption.Required))
- 112 {
- 113 if
- (Entity !=
- null)
- 114 {
- 115 foreach
- (
- var
- items
- in Entity)
- 116 {
- 117 var
- EntityModel =
- _DbContextHandle.Entry(Entity);
- 118
- _DbContextHandle.Set
- ().Attach(items);
- 119
- EntityModel.State =
- EntityState.Modified;
- 120 }
- 121
- 122 }
- 123
- Count =
- _DbContextHandle.SaveChanges();
- 124 Ts.Complete();
- 125 }
- 126
- 127 return
- Count >
- 0;
- 128 }
- 129
- 130
- 131 /// <summary>
- 132 /// 进行修改单个实体对象
- 133 /// </summary>
- 134 /// <typeparam name="T"></typeparam>
- 135 /// <param name="Entity">实体对象</param>
- 136 /// <returns></returns>
- 137 public bool
- Update(T Entity)
- where
- T :
- class
- 138 {
- 139 using
- (TransactionScope Ts =
- new TransactionScope())
- 140 {
- 141 var
- EntityModel = _DbContextHandle.Entry
- (Entity);
- 142
- _DbContextHandle.Set
- ().Attach(Entity);
- 143
- EntityModel.State =
- EntityState.Modified;
- 144 int
- Count =
- _DbContextHandle.SaveChanges();
- 145 Ts.Complete();
- 146 return
- Count >
- 0;
- 147 }
- 148 }
- 149
- 150 /// <summary>
- 151 /// 批量的修改
- 152 /// </summary>
- 153 /// <typeparam name="T"></typeparam>
- 154 /// <param name="WhereLambda"></param>
- 155 /// <param name="UpdateLambda"></param>
- 156 /// <returns></returns>
- 157 public bool
- Update(Expression
- bool
- >> WhereLambda, Expression> UpdateLambda)
- where
- T :
- class
- 158 {
- 159
- _DbContextHandle.Set().Where(WhereLambda).Update
- (UpdateLambda);
- 160 return
- _DbContextHandle.SaveChanges() >
- 0;
- 161 }
- 162
- 163
- 164 /// <summary>
- 165 /// 查询条件进行修改
- 166 /// </summary>
- 167 /// <typeparam name="T"></typeparam>
- 168 /// <param name="model"></param>
- 169 /// <param name="WhereLambda"></param>
- 170 /// <param name="ModifiedProNames"></param>
- 171 /// <returns></returns>
- 172 public bool
- Update(T model, Expression
- bool
- >> WhereLambda,
- params string
- [] ModifiedProNames)
- where
- T :
- class
- 173 {
- 174 //查询要修改的数据
- 175
- List ListModifing = _DbContextHandle.Set
- ().Where(WhereLambda).ToList();
- 176
- Type t =
- typeof(T);
- 177
- List ProInfos = t.GetProperties(BindingFlags.Instance |
- BindingFlags.Public).ToList();
- 178
- Dictionary<
- string
- , PropertyInfo> DitProList =
- new
- Dictionary<
- string
- , PropertyInfo>
- ();
- 179
- ProInfos.ForEach(p =>
- 180 {
- 181 if (ModifiedProNames.Contains(p.Name))
- 182 {
- 183 DitProList.Add(p.Name, p);
- 184 }
- 185 });
- 186
- 187 if
- (DitProList.Count <=
- 0)
- 188 {
- 189 throw new
- Exception(
- "指定修改的字段名称有误或为空");
- 190 }
- 191 foreach
- (
- var
- item
- in DitProList)
- 192 {
- 193
- PropertyInfo proInfo =
- item.Value;
- 194 object
- newValue = proInfo.GetValue(model,
- null);
- 195 //批量进行修改相互对应的属性
- 196 foreach
- (T oModel
- in ListModifing)
- 197 {
- 198
- proInfo.SetValue(oModel, newValue,
- null
- );
- //设置其中新的值
- 199 }
- 200 }
- 201
- 202 return
- _DbContextHandle.SaveChanges() >
- 0;
- 203 }
- 204 /// <summary>
- 205 /// 释放缓存
- 206 /// </summary>
- 207 public void Dispose()
- 208 {
- 209 _DbContextHandle.Dispose();
- 210 }
- 211
- 212 /// <summary>
- 213 /// 查询单个对象
- 214 /// </summary>
- 215 /// <typeparam name="T"></typeparam>
- 216 /// <param name="ID">主键ID</param>
- 217 /// <returns></returns>
- 218 public
- T FindByID(
- dynamic
- ID)
- where
- T :
- class
- 219 {
- 220 return
- _DbContextHandle.Set().Find(ID) ??
- null;
- 221 }
- 222
- 223
- 224 /// <summary>
- 225 /// 获取全部数据的列表
- 226 /// </summary>
- 227 /// <typeparam name="T"></typeparam>
- 228 /// <param name="Order">排序</param>
- 229 /// <returns></returns>
- 230 public
- List GetAll(
- string
- Order =
- null
- )
- where
- T :
- class
- 231 {
- 232 return
- Order !=
- null
- _DbContextHandle.Set().OrderBy(Order).ToList() ??
- null
- : _DbContextHandle.Set().ToList() ??
- null;
- 233 }
- 234
- 235 /// <summary>
- 236 ///根据查询条件进行查询列表
- 237 /// </summary>
- 238 /// <typeparam name="T"></typeparam>
- 239 /// <param name="WhereLambda"></param>
- 240 /// <returns></returns>
- 241 public
- List GetAllQuery(Expression
- bool
- >> WhereLambda =
- null
- )
- where
- T :
- class
- 242 {
- 243 return
- WhereLambda !=
- null
- _DbContextHandle.Set().Where(WhereLambda).ToList() ??
- null
- : _DbContextHandle.Set().ToList() ??
- null;
- 244 }
- 245
- 246 /// <summary>
- 247 ///判断对象是否存在
- 248 /// </summary>
- 249 /// <typeparam name="T"></typeparam>
- 250 /// <param name="WhereLambda"></param>
- 251 /// <returns></returns>
- 252 public bool
- GetAny(Expression
- bool
- >> WhereLambda =
- null
- )
- where
- T :
- class
- 253 {
- 254 return
- WhereLambda !=
- null
- _DbContextHandle.Set().Where(WhereLambda).Any() : _DbContextHandle.Set
- ().Any();
- 255 }
- 256
- 257 /// <summary>
- 258 /// 获取查询条件的记录数
- 259 /// </summary>
- 260 /// <typeparam name="T"></typeparam>
- 261 /// <param name="WhereLambda"></param>
- 262 /// <returns></returns>
- 263 public int
- GetCount(Expression
- bool
- >> WhereLambda =
- null
- )
- where
- T :
- class
- 264 {
- 265 return
- WhereLambda !=
- null
- _DbContextHandle.Set().Where(WhereLambda).Count() : _DbContextHandle.Set
- ().Count();
- 266 }
- 267
- 268
- 269 /// <summary>
- 270 /// 获取单条的记录
- 271 /// </summary>
- 272 /// <typeparam name="T"></typeparam>
- 273 /// <param name="WhereLambda"></param>
- 274 /// <returns></returns>
- 275 public
- T GetFristDefault(Expression
- bool
- >> WhereLambda =
- null
- )
- where
- T :
- class
- 276 {
- 277 return
- WhereLambda !=
- null
- _DbContextHandle.Set().Where(WhereLambda).FirstOrDefault() ??
- null
- : _DbContextHandle.Set().FirstOrDefault() ??
- null;
- 278 }
- 279
- 280
- 281 /// <summary>
- 282 /// 查询对象的转化
- 283 /// </summary>
- 284 /// <typeparam name="T"></typeparam>
- 285 /// <param name="WhereLambda"></param>
- 286 /// <returns></returns>
- 287 public
- List GetSelect(Expression
- bool
- >> WhereLambda)
- where
- T :
- class
- 288 {
- 289 return
- _DbContextHandle.Set().Where(WhereLambda).ToList() ??
- null;
- 290 }
- 291
- 292 /// <summary>
- 293 ///根据查询条件进行分页
- 294 /// </summary>
- 295 /// <typeparam name="T"></typeparam>
- 296 /// <param name="PageIndex">当前页</param>
- 297 /// <param name="PageSize">每页的大小</param>
- 298 /// <param name="TotalCount">总记录数</param>
- 299 /// <param name="ordering">排序条件</param>
- 300 /// <param name="WhereLambda">查询条件</param>
- 301 /// <returns></returns>
- 302 public
- List Pagination(
- int
- PageIndex,
- int
- PageSize,
- out int
- TotalCount,
- string
- Ordering, Expression
- bool
- >> WhereLambda =
- null
- )
- where
- T :
- class
- 303 {
- 304 //分页的时候一定要注意 Order 一定在Skip 之前
- 305 var
- QueryList = _DbContextHandle.Set
- ().OrderBy(Ordering);
- 306 if
- (WhereLambda !=
- null)
- 307 {
- 308
- QueryList =
- QueryList.Where(WhereLambda);
- 309 }
- 310
- 311
- TotalCount =
- QueryList.Count();
- 312 return
- QueryList.Skip(PageSize * (PageIndex -
- 1
- )).Take(PageSize).ToList()
- null;
- 313 }
- 314
- 315 /// <summary>
- 316 ///根据查询条件进行分页
- 317 /// </summary>
- 318 /// <typeparam name="T"></typeparam>
- 319 /// <param name="PageIndex">当前页</param>
- 320 /// <param name="PageSize">每页的大小</param>
- 321 /// <param name="TotalCount">总记录数</param>
- 322 /// <param name="OrderBy">排序条件</param>
- 323 /// <param name="WhereLambda">查询的条件</param>
- 324 /// <param name="IsOrder"></param>
- 325 /// <returns></returns>
- 326 public
- List Pagination(
- int
- PageIndex,
- int
- PageSize,
- out int
- TotalCount, Expression> OrderBy, Expression
- bool
- >> WhereLambda =
- null
- ,
- bool
- IsOrder =
- true
- )
- where
- T :
- class
- 327 {
- 328 //分页的时候一定要注意 Order一定在Skip 之前
- 329
- IQueryable QueryList = IsOrder ==
- true
- _DbContextHandle.Set().OrderBy(OrderBy) : _DbContextHandle.Set
- ().OrderByDescending(OrderBy);
- 330
- 331 if
- (WhereLambda !=
- null)
- 332 {
- 333
- QueryList =
- QueryList.Where(WhereLambda);
- 334 }
- 335
- 336
- TotalCount =
- QueryList.Count();
- 337 return
- QueryList.Skip(PageSize * (PageIndex -
- 1
- )).Take(PageSize).ToList()
- null;
- 338 }
- 339
- 340
- 341 /// <summary>
- 342 /// 执行存储过程的SQL 语句
- 343 /// </summary>
- 344 /// <typeparam name="T"></typeparam>
- 345 /// <param name="Sql">执行的SQL语句</param>
- 346 /// <param name="Parms">SQL 语句的参数</param>
- 347 /// <param name="CmdType"></param>
- 348 /// <returns></returns>
- 349 public
- List QueryPro(
- string
- Sql, List Parms, CommandType CmdType = CommandType.Text)
- where
- T :
- class
- 350 {
- 351 //进行执行存储过程
- 352 if
- (CmdType ==
- CommandType.StoredProcedure)
- 353 {
- 354
- StringBuilder paraNames =
- new StringBuilder();
- 355 foreach
- (
- var
- item
- in Parms)
- 356 {
- 357
- paraNames.Append($
- " @{item},");
- 358 }
- 359
- Sql = paraNames.Length >
- 0
- $
- "exec {Sql} {paraNames.ToString().Trim(',')}"
- : $
- "exec {Sql} ";
- 360 }
- 361 return
- _DbContextHandle.Set
- ().SqlQuery(Sql, Parms.ToArray()).ToList();
- 362 }
- 363
- 364
- 365 /// <summary>
- 366 /// 进行回滚
- 367 /// </summary>
- 368 /// <typeparam name="T"></typeparam>
- 369 public void
- RollBackChanges()
- where
- T :
- class
- 370 {
- 371 var
- Query =
- _DbContextHandle.ChangeTracker.Entries().ToList();
- 372
- 373
- Query.ForEach(p => p.State =
- EntityState.Unchanged);
- 374 }
- 375
- 376 }
- 377
- }
来源: http://www.cnblogs.com/LowKeyCXY/p/7170728.html