• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

C#EF操作类

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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 }  

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Struct和Class的区别C#之结构struct(转)发布时间:2022-07-13
下一篇:
c#实现定义一套中间SQL可以跨库执行的SQL语句发布时间:2022-07-13
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap