Skip to content

Commit 2eb509a

Browse files
author
zzzprojects
committed
Fix: Use InterceptionContext for EF6 for BatchDelete && BatchUpdate
Fix: Use InterceptionContext for EF6 for BatchDelete && BatchUpdate
1 parent 43ddc8b commit 2eb509a

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

src/shared/Z.EF.Plus.BatchDelete.Shared/BatchDelete.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#elif EF6
2222
using System.Data.Entity.Core.Objects;
23+
using System.Data.Entity.Infrastructure.Interception;
2324
using Z.EntityFramework.Plus.Internal.Core.SchemaObjectModel;
2425

2526
#elif EFCORE
@@ -173,7 +174,8 @@ public int Execute<T>(IQueryable<T> query) where T : class
173174

174175
// GET model and info
175176
#if EF5 || EF6
176-
var model = query.GetDbContext().GetModel();
177+
var dbContext = query.GetDbContext();
178+
var model = dbContext.GetModel();
177179
var entity = model.Entity<T>();
178180
var keys = entity.Info.Key.PropertyRefs;
179181

@@ -206,6 +208,7 @@ public int Execute<T>(IQueryable<T> query) where T : class
206208
Executing(command);
207209
}
208210

211+
#if EF5
209212
if (command.GetType().Name == "NpgsqlCommand")
210213
{
211214
command.CommandText = command.CommandText.Replace("[", "\"").Replace("]", "\"");
@@ -232,6 +235,36 @@ public int Execute<T>(IQueryable<T> query) where T : class
232235
var rowAffecteds = (int)command.ExecuteScalar();
233236
return rowAffecteds;
234237
}
238+
#elif EF6
239+
var interceptionContext = new DbCommandInterceptionContext(dbContext.GetObjectContext().GetInterceptionContext());
240+
241+
if (command.GetType().Name == "NpgsqlCommand")
242+
{
243+
command.CommandText = command.CommandText.Replace("[", "\"").Replace("]", "\"");
244+
int totalRowAffecteds = DbInterception.Dispatch.Command.NonQuery(command, interceptionContext);
245+
return totalRowAffecteds;
246+
}
247+
else if (command.Connection.GetType().Name.Contains("MySql"))
248+
{
249+
int totalRowAffecteds = DbInterception.Dispatch.Command.NonQuery(command, interceptionContext);
250+
return totalRowAffecteds;
251+
}
252+
else if (command.Connection.GetType().Name.Contains("Oracle"))
253+
{
254+
int totalRowAffecteds = DbInterception.Dispatch.Command.NonQuery(command, interceptionContext);
255+
return totalRowAffecteds;
256+
}
257+
else if (command.GetType().Name == "SqlCeCommand")
258+
{
259+
int totalRowAffecteds = DbInterception.Dispatch.Command.NonQuery(command, interceptionContext);
260+
return totalRowAffecteds;
261+
}
262+
else
263+
{
264+
var rowAffecteds = (int)DbInterception.Dispatch.Command.Scalar(command, interceptionContext);
265+
return rowAffecteds;
266+
}
267+
#endif
235268

236269
}
237270
finally
@@ -300,7 +333,7 @@ public int Execute<T>(IQueryable<T> query) where T : class
300333
}
301334
}
302335
#endif
303-
}
336+
}
304337

305338
#if EF5 || EF6
306339
/// <summary>Creates a command to execute the batch operation.</summary>

src/shared/Z.EF.Plus.BatchUpdate.Shared/BatchUpdate.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#elif EF6
2020
using System.Data.Entity.Core.Objects;
21+
using System.Data.Entity.Infrastructure.Interception;
2122
using Z.EntityFramework.Plus.Internal.Core.SchemaObjectModel;
2223

2324
#elif EFCORE
@@ -172,7 +173,8 @@ public int Execute<T>(IQueryable<T> query, Expression<Func<T, T>> updateFactory)
172173
var objectQuery = query.GetObjectQuery();
173174

174175
// GET model and info
175-
var model = query.GetDbContext().GetModel();
176+
var dbContext = query.GetDbContext();
177+
var model = dbContext.GetModel();
176178
var entity = model.Entity<T>();
177179

178180
// TODO: Select only key + lambda columns
@@ -209,7 +211,12 @@ public int Execute<T>(IQueryable<T> query, Expression<Func<T, T>> updateFactory)
209211
Executing(command);
210212
}
211213

214+
#if EF5
212215
var rowAffecteds = command.ExecuteNonQuery();
216+
#elif EF6
217+
var interceptionContext = new DbCommandInterceptionContext(dbContext.GetObjectContext().GetInterceptionContext());
218+
var rowAffecteds = DbInterception.Dispatch.Command.NonQuery(command, interceptionContext);
219+
#endif
213220
return rowAffecteds;
214221
}
215222
finally
@@ -289,7 +296,7 @@ public int Execute<T>(IQueryable<T> query, Expression<Func<T, T>> updateFactory)
289296
}
290297
}
291298
#endif
292-
}
299+
}
293300

294301
#if EF5 || EF6
295302
/// <summary>Creates a command to execute the batch operation.</summary>

version.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.6.8
1+
v1.6.9

0 commit comments

Comments
 (0)