在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
Repository.cs
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Data; 5 using System.Data.Entity; 6 using Models; 7 using System.Linq.Expressions; 8 using System.Reflection; 9 using System.Web; 10 using System.Data.OracleClient; 11 12 namespace DAL 13 { 14 public class SortExpression<TEntity, TType> 15 { 16 Expression<Func<TEntity, TType>> SortProperty; 17 } 18 19 public class GenericRepository<TEntity> where TEntity : class 20 { 21 private Pub p_function = new Pub(); 22 23 internal LrBid context; 24 internal DbSet<TEntity> dbSet; 25 26 public GenericRepository(LrBid context) 27 { 28 this.context = context; 29 this.dbSet = context.Set<TEntity>(); 30 } 31 32 public virtual IEnumerable<TEntity> Get( 33 Expression<Func<TEntity, bool>> filter = null, 34 Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null, 35 string includeProperties = "") 36 { 37 IQueryable<TEntity> query = dbSet; 38 39 if (filter != null) 40 { 41 query = query.Where(filter); 42 } 43 44 foreach (var includeProperty in includeProperties.Split 45 (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries)) 46 { 47 query = query.Include(includeProperty); 48 } 49 50 if (orderBy != null) 51 { 52 return orderBy(query).ToList(); 53 } 54 else 55 { 56 return query.ToList(); 57 } 58 } 59 60 public virtual TEntity GetByID(object id) 61 { 62 return dbSet.Find(id); 63 } 64 65 public virtual void Insert(TEntity entity) 66 { 67 dbSet.Add(entity); 68 } 69 70 public virtual void Delete(object id) 71 { 72 TEntity entityToDelete = dbSet.Find(id); 73 Delete(entityToDelete); 74 } 75 76 public virtual void Delete(TEntity entityToDelete) 77 { 78 if (context.Entry(entityToDelete).State == EntityState.Detached) 79 { 80 dbSet.Attach(entityToDelete); 81 } 82 dbSet.Remove(entityToDelete); 83 } 84 85 public virtual void Update(TEntity entityToUpdate) 86 { 87 dbSet.Attach(entityToUpdate); 88 context.Entry(entityToUpdate).State = EntityState.Modified; 89 } 90 91 92 /// <summary> 93 /// 有跟踪状态 94 /// </summary> 95 /// <param name="query">SQL查询语句,注意ORCALE参数以:为标记,MS SQL以@为标记</param> 96 /// <param name="parameters">参数</param> 97 /// <returns></returns> 98 public virtual IEnumerable<TEntity> GetWithRawSql(string query, params object[] parameters) 99 { 100 return dbSet.SqlQuery(query, parameters).ToList(); 101 } 102 103 // 104 /// <summary> 105 /// 没有跟踪状态 106 /// </summary> 107 /// <param name="query"></param> 108 /// <param name="parameters"></param> 109 /// <returns></returns> 110 public virtual IEnumerable<TEntity> GetwhithdbSql(string query, params object[] parameters) 111 { 112 return context.Database.SqlQuery<TEntity>(query, parameters); 113 } 114 115 /// <summary> 116 /// 获取单行数据,多用于统计 117 /// </summary> 118 /// <param name="query"></param> 119 /// <param name="parameters"></param> 120 /// <returns></returns> 121 public virtual TEntity GetwhithdbSqlSingle(string query, params object[] parameters) 122 { 123 return context.Database.SqlQuery<TEntity>(query, parameters).FirstOrDefault(); 124 } 125 126 } 127 }
UnitOfWork.cs
1 using System; 2 using Models; 3 4 namespace DAL 5 { 6 public class UnitOfWork : IDisposable 7 { 8 9 //T_ATTACHMENT 10 private GenericRepository<T_ATTACHMENT> t_attachment_Repository; 11 12 public GenericRepository<T_ATTACHMENT> T_ATTACHMENT_Repository 13 { 14 get 15 { 16 17 if (this.t_attachment_Repository == null) 18 { 19 this.t_attachment_Repository = new GenericRepository<T_ATTACHMENT>(context); 20 } 21 return t_attachment_Repository; 22 } 23 } 24 25 public void Save() 26 { 27 context.SaveChanges(); 28 } 29 30 private bool disposed = false; 31 32 protected virtual void Dispose(bool disposing) 33 { 34 if (!this.disposed) 35 { 36 if (disposing) 37 { 38 context.Dispose(); 39 } 40 } 41 this.disposed = true; 42 } 43 44 public void Dispose() 45 { 46 Dispose(true); 47 GC.SuppressFinalize(this); 48 } 49 50 } 51 }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论