Skip to content

Commit e020257

Browse files
author
Francinaldo Portela
committed
Implemented in memory check for Effor in EF6
1 parent 3769243 commit e020257

File tree

6 files changed

+76
-2
lines changed

6 files changed

+76
-2
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,23 @@
3737
<Prefer32Bit>false</Prefer32Bit>
3838
</PropertyGroup>
3939
<ItemGroup>
40+
<Reference Include="Effort, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6a46696d54971e6d, processorArchitecture=MSIL">
41+
<HintPath>..\packages\Effort.EF6.1.3.0\lib\net45\Effort.dll</HintPath>
42+
<Private>True</Private>
43+
</Reference>
4044
<Reference Include="EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089, processorArchitecture=MSIL">
4145
<HintPath>..\packages\EntityFramework.6.0.0\lib\net45\EntityFramework.dll</HintPath>
4246
</Reference>
47+
<Reference Include="NMemory, Version=1.0.0.0, Culture=neutral, PublicKeyToken=6a46696d54971e6d, processorArchitecture=MSIL">
48+
<HintPath>..\packages\NMemory.1.1.0\lib\net45\NMemory.dll</HintPath>
49+
<Private>True</Private>
50+
</Reference>
4351
<Reference Include="System" />
4452
<Reference Include="System.ComponentModel.DataAnnotations" />
4553
<Reference Include="System.Core" />
4654
<Reference Include="System.Runtime.Caching" />
4755
<Reference Include="System.Runtime.Extensions" />
56+
<Reference Include="System.Transactions" />
4857
<Reference Include="System.Xml.Linq" />
4958
<Reference Include="System.Data.DataSetExtensions" />
5059
<Reference Include="Microsoft.CSharp" />
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Effort.EF6" version="1.3.0" targetFramework="net45" />
34
<package id="EntityFramework" version="6.0.0" targetFramework="net45" />
5+
<package id="NMemory" version="1.1.0" targetFramework="net45" />
46
</packages>

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 is 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)