1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186 |
using
System; using
System.Collections.Generic; using
System.Data.Entity.Infrastructure; using
System.Linq; using
System.Linq.Expressions; using
System.Reflection; using
System.Text; namespace
P04DAL { public
class BaseDAL<T> where
T: class , new () { /// <summary> /// 数据上下文对象 /// </summary> P05MODEL.LeaveWordBoradEntities db = new
P05MODEL.LeaveWordBoradEntities(); #region 1.0 新增 实体 +int Add(T model) /// <summary> /// 新增 实体 /// </summary> /// <param name="model"></param> /// <returns></returns> public
int Add(T model) { db.Set<T>().Add(model); return
db.SaveChanges(); //保存成功后,会将自增的id设置给 model的 主键属性,并返回受影响行数 } #endregion #region 2.0 根据 id 删除 +int Del(T model) /// <summary> /// 根据 id 删除 /// </summary> /// <param name="model">包含要删除id的对象</param> /// <returns></returns> public
int Del(T model) { db.Set<T>().Attach(model); db.Set<T>().Remove(model); return
db.SaveChanges(); } #endregion #region 3.0 根据条件删除 +int DelBy(Expression<Func<T, bool>> delWhere) /// <summary> /// 3.0 根据条件删除 /// </summary> /// <param name="delWhere"></param> /// <returns></returns> public
int DelBy(Expression<Func<T, bool >> delWhere) { //3.1查询要删除的数据 List<T> listDeleting = db.Set<T>().Where(delWhere).ToList(); //3.2将要删除的数据 用删除方法添加到 EF 容器中 listDeleting.ForEach(u => { db.Set<T>().Attach(u); //先附加到 EF容器 db.Set<T>().Remove(u); //标识为 删除 状态 }); //3.3一次性 生成sql语句到数据库执行删除 return
db.SaveChanges(); } #endregion #region 4.0 修改 +int Modify(T model, params string[] proNames) /// <summary> /// 4.0 修改,如: /// T u = new T() { uId = 1, uLoginName = "asdfasdf" }; /// this.Modify(u, "uLoginName"); /// </summary> /// <param name="model">要修改的实体对象</param> /// <param name="proNames">要修改的 属性 名称</param> /// <returns></returns> public
int Modify(T model, params
string [] proNames) { //4.1将 对象 添加到 EF中 DbEntityEntry entry = db.Entry<T>(model); //4.2先设置 对象的包装 状态为 Unchanged entry.State = System.Data.EntityState.Unchanged; //4.3循环 被修改的属性名 数组 foreach
( string
proName in
proNames) { //4.4将每个 被修改的属性的状态 设置为已修改状态;后面生成update语句时,就只为已修改的属性 更新 entry.Property(proName).IsModified = true ; } //4.4一次性 生成sql语句到数据库执行 return
db.SaveChanges(); } #endregion #region 4.0 批量修改 +int Modify(T model, Expression<Func<T, bool>> whereLambda, params string[] modifiedProNames) /// <summary> /// 4.0 批量修改 /// </summary> /// <param name="model">要修改的实体对象</param> /// <param name="whereLambda">查询条件</param> /// <param name="proNames">要修改的 属性 名称</param> /// <returns></returns> public
int ModifyBy(T model, Expression<Func<T, bool >> whereLambda, params
string [] modifiedProNames) { //4.1查询要修改的数据 List<T> listModifing = db.Set<T>().Where(whereLambda).ToList(); //获取 实体类 类型对象 Type t = typeof (T); // model.GetType(); //获取 实体类 所有的 公有属性 List<PropertyInfo> proInfos = t.GetProperties(BindingFlags.Instance | BindingFlags.Public).ToList(); //创建 实体属性 字典集合 Dictionary< string , PropertyInfo> dictPros = new
Dictionary< string , PropertyInfo>(); //将 实体属性 中要修改的属性名 添加到 字典集合中 键:属性名 值:属性对象 proInfos.ForEach(p => { if
(modifiedProNames.Contains(p.Name)) { dictPros.Add(p.Name, p); } }); //4.3循环 要修改的属性名 foreach
( string
proName in
modifiedProNames) { //判断 要修改的属性名是否在 实体类的属性集合中存在 if
(dictPros.ContainsKey(proName)) { //如果存在,则取出要修改的 属性对象 PropertyInfo proInfo = dictPros[proName]; //取出 要修改的值 object
newValue = proInfo.GetValue(model, null ); //object newValue = model.uName; //4.4批量设置 要修改 对象的 属性 foreach
(T usrO in
listModifing) { //为 要修改的对象 的 要修改的属性 设置新的值 proInfo.SetValue(usrO, newValue, null ); //usrO.uName = newValue; } } } //4.4一次性 生成sql语句到数据库执行 return
db.SaveChanges(); } #endregion #region 5.0 根据条件查询 +List<T> GetListBy(Expression<Func<T,bool>> whereLambda) /// <summary> /// 5.0 根据条件查询 +List<T> GetListBy(Expression<Func<T,bool>> whereLambda) /// </summary> /// <param name="whereLambda"></param> /// <returns></returns> public
List<T> GetListBy(Expression<Func<T, bool >> whereLambda) { return
db.Set<T>().Where(whereLambda).ToList(); } #endregion #region 5.1 根据条件 排序 和查询 + List<T> GetListBy<TKey> /// <summary> /// 5.1 根据条件 排序 和查询 /// </summary> /// <typeparam name="TKey">排序字段类型</typeparam> /// <param name="whereLambda">查询条件 lambda表达式</param> /// <param name="orderLambda">排序条件 lambda表达式</param> /// <returns></returns> public
List<T> GetListBy<TKey>(Expression<Func<T, bool >> whereLambda, Expression<Func<T, TKey>> orderLambda) { return
db.Set<T>().Where(whereLambda).OrderBy(orderLambda).ToList(); } #endregion #region 6.0 分页查询 + List<T> GetPagedList<TKey> /// <summary> /// 6.0 分页查询 + List<T> GetPagedList<TKey> /// </summary> /// <param name="pageIndex">页码</param> /// <param name="pageSize">页容量</param> /// <param name="whereLambda">条件 lambda表达式</param> /// <param name="orderBy">排序 lambda表达式</param> /// <returns></returns> public
List<T> GetPagedList<TKey>( int
pageIndex, int
pageSize, Expression<Func<T, bool >> whereLambda, Expression<Func<T, TKey>> orderBy) { // 分页 一定注意: Skip 之前一定要 OrderBy return
db.Set<T>().Where(whereLambda).OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); } #endregion } } |
EF5 通用数据层 增删改查操作,泛型类,布布扣,bubuko.com
原文:http://www.cnblogs.com/12taotie21/p/3650862.html