Skip to content

Commit b101ba7

Browse files
author
zzzprojects
committed
Add support to InMemory Batch Delete for EFCore
Add support to InMemory Batch Delete for EFCore
1 parent 850e724 commit b101ba7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2334
-57
lines changed

src/Z.EntityFramework.Plus.EF6/BatchDelete/BatchDelete.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,16 @@ public int Execute<T>(IQueryable<T> query) where T : class
187187
}
188188
}
189189
#elif EFCORE
190+
if (BatchDeleteManager.InMemoryDbContextFactory != null && query.IsInMemoryQueryContext())
191+
{
192+
var context = BatchDeleteManager.InMemoryDbContextFactory();
193+
194+
var list = query.ToList();
195+
context.RemoveRange(list);
196+
context.SaveChanges();
197+
return list.Count;
198+
}
199+
190200
var dbContext = query.GetDbContext();
191201
var entity = dbContext.Model.FindEntityType(typeof(T));
192202
var keys = entity.GetKeys().ToList()[0].Properties;

src/Z.EntityFramework.Plus.EF6/BatchDelete/BatchDeleteManager.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

88
using System;
9+
#if EFCORE
10+
using Microsoft.EntityFrameworkCore;
11+
#endif
912

1013
namespace Z.EntityFramework.Plus
1114
{
@@ -15,5 +18,12 @@ public class BatchDeleteManager
1518
/// <summary>Gets or sets the batch delete builder to change default configuration.</summary>
1619
/// <value>The batch delete builder to change default configuration.</value>
1720
public static Action<BatchDelete> BatchDeleteBuilder { get; set; }
21+
22+
#if EFCORE
23+
24+
/// <summary>Gets or sets the factory to create an InMemory DbContext.</summary>
25+
/// <value>The factory to create an InMemory DbContext.</value>
26+
public static Func<DbContext> InMemoryDbContextFactory { get; set; }
27+
#endif
1828
}
1929
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@
183183
<Compile Include="_Internal\EFCore\IQueryable`\CreateCommand.cs" />
184184
<Compile Include="_Internal\EFCore\IQueryable`\GetDbContext.cs" />
185185
<Compile Include="_Internal\EFCore\IQueryable`\IQueryable`.GetCommand.cs" />
186+
<Compile Include="_Internal\EFCore\IQueryable`\IsInMemoryQueryContext.cs" />
186187
<Compile Include="_Internal\EF\DbContext\DbContext.GetDbSetProperties.cs" />
187188
<Compile Include="_Internal\EF\DbContext\DbContext.MapReader.cs" />
188189
<Compile Include="_Internal\EF\IQueryable`\IQueryable.Order.cs" />
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit)
2+
// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus
3+
// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues
4+
// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
7+
8+
#if FULL
9+
#if EFCORE
10+
11+
using System;
12+
using System.Linq;
13+
using System.Reflection;
14+
using Microsoft.EntityFrameworkCore;
15+
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
16+
using Microsoft.EntityFrameworkCore.Infrastructure;
17+
using Microsoft.EntityFrameworkCore.Internal;
18+
using Microsoft.EntityFrameworkCore.Query;
19+
using Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal;
20+
using Microsoft.EntityFrameworkCore.Query.Internal;
21+
using Microsoft.EntityFrameworkCore.Storage;
22+
using Remotion.Linq.Parsing.ExpressionVisitors.TreeEvaluation;
23+
using Remotion.Linq.Parsing.Structure;
24+
25+
namespace Z.EntityFramework.Plus
26+
{
27+
internal static partial class InternalExtensions
28+
{
29+
public static bool IsInMemoryQueryContext<T>(this IQueryable<T> query)
30+
{
31+
var compilerField = typeof(EntityQueryProvider).GetField("_queryCompiler", BindingFlags.NonPublic | BindingFlags.Instance);
32+
var compiler = (QueryCompiler)compilerField.GetValue(query.Provider);
33+
34+
var queryContextFactoryField = compiler.GetType().GetField("_queryContextFactory", BindingFlags.NonPublic | BindingFlags.Instance);
35+
var queryContextFactory = queryContextFactoryField.GetValue(compiler);
36+
37+
var relationalQueryContextFactory = queryContextFactory as RelationalQueryContextFactory;
38+
39+
return relationalQueryContextFactory == null && queryContextFactory.GetType().Name == "InMemoryQueryContextFactory";
40+
}
41+
}
42+
}
43+
44+
#endif
45+
#endif

src/Z.EntityFramework.Plus.EFCore/BatchDelete/BatchDelete.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,16 @@ public int Execute<T>(IQueryable<T> query) where T : class
187187
}
188188
}
189189
#elif EFCORE
190+
if (BatchDeleteManager.InMemoryDbContextFactory != null && query.IsInMemoryQueryContext())
191+
{
192+
var context = BatchDeleteManager.InMemoryDbContextFactory();
193+
194+
var list = query.ToList();
195+
context.RemoveRange(list);
196+
context.SaveChanges();
197+
return list.Count;
198+
}
199+
190200
var dbContext = query.GetDbContext();
191201
var entity = dbContext.Model.FindEntityType(typeof(T));
192202
var keys = entity.GetKeys().ToList()[0].Properties;

src/Z.EntityFramework.Plus.EFCore/BatchDelete/BatchDeleteManager.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

88
using System;
9+
#if EFCORE
10+
using Microsoft.EntityFrameworkCore;
11+
#endif
912

1013
namespace Z.EntityFramework.Plus
1114
{
@@ -15,5 +18,12 @@ public class BatchDeleteManager
1518
/// <summary>Gets or sets the batch delete builder to change default configuration.</summary>
1619
/// <value>The batch delete builder to change default configuration.</value>
1720
public static Action<BatchDelete> BatchDeleteBuilder { get; set; }
21+
22+
#if EFCORE
23+
24+
/// <summary>Gets or sets the factory to create an InMemory DbContext.</summary>
25+
/// <value>The factory to create an InMemory DbContext.</value>
26+
public static Func<DbContext> InMemoryDbContextFactory { get; set; }
27+
#endif
1828
}
1929
}

src/Z.EntityFramework.Plus.EFCore/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@
1818
[assembly: AssemblyCulture("")]
1919
[assembly: ComVisible(false)]
2020
[assembly: Guid("c41195f5-937e-4f2b-9f36-20650a804caa")]
21-
[assembly: AssemblyVersion("1.3.0")]
22-
[assembly: AssemblyFileVersion("1.3.0")]
21+
[assembly: AssemblyVersion("1.3.1")]
22+
[assembly: AssemblyFileVersion("1.3.1")]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit)
2+
// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus
3+
// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues
4+
// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
7+
8+
#if FULL || BATCH_DELETE
9+
#if EFCORE
10+
11+
using System;
12+
using System.Linq;
13+
using System.Reflection;
14+
using Microsoft.EntityFrameworkCore;
15+
using Microsoft.EntityFrameworkCore.ChangeTracking.Internal;
16+
using Microsoft.EntityFrameworkCore.Infrastructure;
17+
using Microsoft.EntityFrameworkCore.Internal;
18+
using Microsoft.EntityFrameworkCore.Query;
19+
using Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal;
20+
using Microsoft.EntityFrameworkCore.Query.Internal;
21+
using Microsoft.EntityFrameworkCore.Storage;
22+
using Remotion.Linq.Parsing.ExpressionVisitors.TreeEvaluation;
23+
using Remotion.Linq.Parsing.Structure;
24+
25+
namespace Z.EntityFramework.Plus
26+
{
27+
internal static partial class InternalExtensions
28+
{
29+
public static bool IsInMemoryQueryContext<T>(this IQueryable<T> query)
30+
{
31+
var compilerField = typeof(EntityQueryProvider).GetField("_queryCompiler", BindingFlags.NonPublic | BindingFlags.Instance);
32+
var compiler = (QueryCompiler)compilerField.GetValue(query.Provider);
33+
34+
var queryContextFactoryField = compiler.GetType().GetField("_queryContextFactory", BindingFlags.NonPublic | BindingFlags.Instance);
35+
var queryContextFactory = queryContextFactoryField.GetValue(compiler);
36+
37+
var relationalQueryContextFactory = queryContextFactory as RelationalQueryContextFactory;
38+
39+
return relationalQueryContextFactory == null && queryContextFactory.GetType().Name == "InMemoryQueryContextFactory";
40+
}
41+
}
42+
}
43+
44+
#endif
45+
#endif

src/Z.EntityFramework.Plus.EFCore/project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.3.0",
2+
"version": "1.3.1",
33
"description": "Description: EF Bulk Operations & Utilities | Bulk Insert, Update, Delete, Merge from database.",
44
"authors": [ "ZZZ Projects Inc." ],
55
"packOptions": {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Description: Entity Framework Bulk Operations & Utilities (EF Bulk SaveChanges, Insert, Update, Delete, Merge | LINQ Query Cache, Deferred, Filter, IncludeFilter, IncludeOptimize | Audit)
2+
// Website & Documentation: https://github.com/zzzprojects/Entity-Framework-Plus
3+
// Forum & Issues: https://github.com/zzzprojects/EntityFramework-Plus/issues
4+
// License: https://github.com/zzzprojects/EntityFramework-Plus/blob/master/LICENSE
5+
// More projects: http://www.zzzprojects.com/
6+
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
7+
8+
using Microsoft.VisualStudio.TestTools.UnitTesting;
9+
10+
namespace Z.Test.EntityFramework.Plus
11+
{
12+
[TestClass]
13+
public partial class BatchDelete_InMemory
14+
{
15+
}
16+
}

0 commit comments

Comments
 (0)