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日 星期四 EFCommon.SqlHelp 21 { Repository : IRepository, IDisposable 23 { DbContext _DbContextHandle = 添加一个对象 Add<T>(T Entity) where T : class 34 { 35 using (TransactionScope Ts = new TransactionScope(TransactionScopeOption.Required)) 36 { 37 _DbContextHandle.Set<T>().Add(Entity); 38 int Count = _DbContextHandle.SaveChanges(); 39 Ts.Complete(); 40 return Count > 0; 41 } 42 } 批量的插入数据 AddRange<T>(List<T> Entity) where T : class 51 { 52 using (TransactionScope Ts = new TransactionScope(TransactionScopeOption.Required)) 53 { 54 _DbContextHandle.Set<T>().AddRange(Entity); 55 int Count = _DbContextHandle.SaveChanges(); 56 Ts.Complete(); 57 return Count > 0; 58 } 59 } 根据查询条件进行删除对象 Delete<T>(Expression<Func<T, bool>> whereLambda) where T : class 68 { 69 using (TransactionScope Ts = new TransactionScope(TransactionScopeOption.Required)) 70 { 71 var EntityModel = _DbContextHandle.Set<T>().Where(whereLambda).FirstOrDefault(); 72 if (EntityModel != null) 73 { 74 _DbContextHandle.Set<T>().Remove(EntityModel); 75 int Count = _DbContextHandle.SaveChanges(); 76 Ts.Complete(); 77 return Count > 0; 78 } ; 80 } 81 } 删除单个对象的实体 Delete<T>(T Entity) where T : class 91 { 92 using (TransactionScope Ts = new TransactionScope(TransactionScopeOption.Required)) 93 { 94 _DbContextHandle.Set<T>().Attach(Entity); 95 _DbContextHandle.Set<T>().Remove(Entity); 96 int Count = _DbContextHandle.SaveChanges(); 97 Ts.Complete(); 98 return Count > 0; 99 } 100 } 批量的进行更新数据 Update<T>(List<T> 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<T>().Attach(items); 119 EntityModel.State = EntityState.Modified; 120 } 121 122 } 123 Count = _DbContextHandle.SaveChanges(); 124 Ts.Complete(); 125 } Count > 0; 128 } 进行修改单个实体对象 Update<T>(T Entity) where T : class 138 { 139 using (TransactionScope Ts = new TransactionScope()) 140 { 141 var EntityModel = _DbContextHandle.Entry<T>(Entity); 142 _DbContextHandle.Set<T>().Attach(Entity); 143 EntityModel.State = EntityState.Modified; 144 int Count = _DbContextHandle.SaveChanges(); 145 Ts.Complete(); 146 return Count > 0; 147 } 148 } 批量的修改 Update<T>(Expression<Func<T, bool>> WhereLambda, Expression<Func<T, T>> UpdateLambda) where T : class 158 { 159 _DbContextHandle.Set<T>().Where(WhereLambda).Update<T>(UpdateLambda); 160 return _DbContextHandle.SaveChanges() > 0; 161 } 查询条件进行修改 Update<T>(T model, Expression<Func<T, { List<T> ListModifing = _DbContextHandle.Set<T>().Where(WhereLambda).ToList(); 176 Type t = typeof(T); 177 List<PropertyInfo> 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 }); (DitProList.Count <= 0) 188 { Exception(); 190 } 191 foreach (var item in DitProList) 192 { 193 PropertyInfo proInfo = item.Value; 194 object newValue = proInfo.GetValue(model, null); (T oModel in ListModifing) 197 { } 200 } _DbContextHandle.SaveChanges() > 0; 203 } 释放缓存 Dispose() 208 { 209 _DbContextHandle.Dispose(); 210 } 查询单个对象 T FindByID<T>(dynamic ID) where T : class 219 { 220 return _DbContextHandle.Set<T>().Find(ID) ?? null; 221 } 获取全部数据的列表 List<T> GetAll<T>(string Order = null) where T : class 231 { 232 return Order != null ? _DbContextHandle.Set<T>().OrderBy(Order).ToList() ?? null : _DbContextHandle.Set<T>().ToList() ?? null; 233 } 根据查询条件进行查询列表 List<T> GetAllQuery<T>(Expression<Func<T, bool>> WhereLambda = null) where T : class 242 { 243 return WhereLambda != null ? _DbContextHandle.Set<T>().Where(WhereLambda).ToList() ?? null : _DbContextHandle.Set<T>().ToList() ?? null; 244 } 判断对象是否存在 GetAny<T>(Expression<Func<T, bool>> WhereLambda = null) where T : class 253 { 254 return WhereLambda != null ? _DbContextHandle.Set<T>().Where(WhereLambda).Any() : _DbContextHandle.Set<T>().Any(); 255 } 获取查询条件的记录数 GetCount<T>(Expression<Func<T, bool>> WhereLambda = null) where T : class 264 { 265 return WhereLambda != null ? _DbContextHandle.Set<T>().Where(WhereLambda).Count() : _DbContextHandle.Set<T>().Count(); 266 } 获取单条的记录 T GetFristDefault<T>(Expression<Func<T, bool>> WhereLambda = null) where T : class 276 { 277 return WhereLambda != null ? _DbContextHandle.Set<T>().Where(WhereLambda).FirstOrDefault() ?? null : _DbContextHandle.Set<T>().FirstOrDefault() ?? null; 278 } 查询对象的转化 List<T> GetSelect<T>(Expression<Func<T, bool>> WhereLambda) where T : class 288 { 289 return _DbContextHandle.Set<T>().Where(WhereLambda).ToList() ?? null; 290 } 根据查询条件进行分页 List<T> Pagination<T>( { QueryList = _DbContextHandle.Set<T>().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 } 根据查询条件进行分页 List<T> Pagination<T, TKey>( { IQueryable<T> QueryList = IsOrder == true ? _DbContextHandle.Set<T>().OrderBy(OrderBy) : _DbContextHandle.Set<T>().OrderByDescending(OrderBy); (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 } 执行存储过程的SQL 语句 List<T> QueryPro<T>(string Sql, List<MySqlParameter> Parms, CommandType CmdType = CommandType.Text) where T : class 350 { (CmdType == CommandType.StoredProcedure) 353 { 354 StringBuilder paraNames = new StringBuilder(); 355 foreach (var item in Parms) 356 { ); 358 } : $; 360 } 361 return _DbContextHandle.Set<T>().SqlQuery(Sql, Parms.ToArray()).ToList(); 362 } 进行回滚 RollBackChanges<T>() where T : class 370 { 371 var Query = _DbContextHandle.ChangeTracker.Entries().ToList(); 372 373 Query.ForEach(p => p.State = EntityState.Unchanged); 374 } 375 376 } 377 }
以上内容 全部是基于原创 部分观点 引用了百度百科 以及个人的对于EF 的理解。如需转载请表明!