Skip to content

Commit ccdfe2b

Browse files
Merge pull request #227 from naldorp/EffortInMemoryFix
Implemented in memory check for Effort in EF6
2 parents 3769243 + e69ecb5 commit ccdfe2b

File tree

5 files changed

+66
-2
lines changed

5 files changed

+66
-2
lines changed

src/Z.EntityFramework.Plus.EF6/Z.EntityFramework.Plus.EF6.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<Reference Include="System.Core" />
4646
<Reference Include="System.Runtime.Caching" />
4747
<Reference Include="System.Runtime.Extensions" />
48+
<Reference Include="System.Transactions" />
4849
<Reference Include="System.Xml.Linq" />
4950
<Reference Include="System.Data.DataSetExtensions" />
5051
<Reference Include="Microsoft.CSharp" />

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,19 @@ public int Execute<T>(IQueryable<T> query) where T : class
185185
{
186186
return 0;
187187
}
188-
188+
189+
#if EF6
190+
if (query.IsInMemoryEffortQueryContext())
191+
{
192+
var context = query.GetDbContext();
193+
194+
var list = query.ToList();
195+
context.Set<T>().RemoveRange(list);
196+
context.SaveChanges();
197+
return list.Count;
198+
}
199+
#endif
200+
189201
// GET model and info
190202
#if EF5 || EF6
191203
var dbContext = query.GetDbContext();

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

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.Linq;
1313
using System.Linq.Expressions;
1414
using System.Text.RegularExpressions;
15+
using System.Reflection;
1516
#if EF5
1617
using System.Data.Objects;
1718
using System.Data.SqlClient;
@@ -184,8 +185,37 @@ public int Execute<T>(IQueryable<T> query, Expression<Func<T, T>> updateFactory)
184185
{
185186
return 0;
186187
}
188+
#if EF6
189+
if (query.IsInMemoryEffortQueryContext())
190+
{
191+
var context = query.GetDbContext();
192+
193+
var list = query.ToList();
194+
var compiled = updateFactory.Compile();
195+
var memberBindings = ((MemberInitExpression)updateFactory.Body).Bindings;
196+
var accessors = memberBindings
197+
.Select(x => x.Member.Name)
198+
.Select(x => new PropertyOrFieldAccessor(typeof(T).GetProperty(x, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)))
199+
.ToList();
200+
201+
foreach (var item in list)
202+
{
203+
var newItem = compiled(item);
204+
205+
foreach (var accessor in accessors)
206+
{
207+
var value = accessor.GetValue(newItem);
208+
accessor.SetValue(item, value);
209+
}
210+
}
211+
212+
context.SaveChanges();
213+
return list.Count;
214+
}
215+
#endif
187216

188217
#if EF5 || EF6
218+
189219
var objectQuery = query.GetObjectQuery();
190220

191221
// GET model and info
@@ -242,6 +272,7 @@ public int Execute<T>(IQueryable<T> query, Expression<Func<T, T>> updateFactory)
242272
innerObjectQuery.Context.Connection.Close();
243273
}
244274
}
275+
245276
#elif EFCORE
246277
if (BatchUpdateManager.InMemoryDbContextFactory != null && query.IsInMemoryQueryContext())
247278
{
@@ -312,7 +343,7 @@ public int Execute<T>(IQueryable<T> query, Expression<Func<T, T>> updateFactory)
312343
}
313344
}
314345
#endif
315-
}
346+
}
316347

317348
#if EF5 || EF6
318349
/// <summary>Creates a command to execute the batch operation.</summary>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+

2+
#if EF6
3+
using System;
4+
using System.Data.Entity;
5+
using System.Linq;
6+
using System.Reflection;
7+
8+
namespace Z.EntityFramework.Plus
9+
{
10+
internal static partial class InternalExtensions
11+
{
12+
public static bool IsInMemoryEffortQueryContext<T>(this IQueryable<T> q)
13+
{
14+
return q.GetDbContext().Database.Connection.GetType().FullName == "Effort.Provider.EffortConnection";
15+
}
16+
}
17+
}
18+
19+
#endif

src/shared/Z.EF.Plus._Core.Shared/Z.EF.Plus._Core.Shared.projitems

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
<Compile Include="$(MSBuildThisFileDirectory)EF5_EF6\ObjectContext\CreateStoreCommand.cs" />
3939
<Compile Include="$(MSBuildThisFileDirectory)EF5_EF6\ObjectContext\GetEntitySet.cs" />
4040
<Compile Include="$(MSBuildThisFileDirectory)EF5_EF6\Object\Object.GetObjectQuery.cs" />
41+
<Compile Include="$(MSBuildThisFileDirectory)EF6\IQueryable`\IsInMemoryEffortQueryContext.cs" />
4142
<Compile Include="$(MSBuildThisFileDirectory)EF6\ObjectContext\GetDbContext.cs" />
4243
<Compile Include="$(MSBuildThisFileDirectory)EF6\ObjectContext\GetInterceptionContext.cs" />
4344
<Compile Include="$(MSBuildThisFileDirectory)EF6\ObjectQuery\GetCommandTextAndParameters.cs" />

0 commit comments

Comments
 (0)