Skip to content

Commit 6c870ca

Browse files
author
zzzprojects
committed
Add IsInMemoryQuery + Fix Take
Add IsInMemoryQuery + Fix Take
1 parent e5873d0 commit 6c870ca

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#if EF5
1616
using System.Data.Objects;
1717
using System.Data.SqlClient;
18+
using System.Reflection;
1819
using Z.EntityFramework.Plus.Internal.Core.SchemaObjectModel;
1920

2021
#elif EF6
@@ -192,6 +193,30 @@ public int Execute<T>(IQueryable<T> query, Expression<Func<T, T>> updateFactory)
192193
}
193194

194195
#if EF5 || EF6
196+
if (BatchUpdateManager.IsInMemoryQuery)
197+
{
198+
var list = query.ToList();
199+
var compiled = updateFactory.Compile();
200+
var memberBindings = ((MemberInitExpression)updateFactory.Body).Bindings;
201+
var accessors = memberBindings
202+
.Select(x => x.Member.Name)
203+
.Select(x => new PropertyOrFieldAccessor(typeof(T).GetProperty(x, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)))
204+
.ToList();
205+
206+
foreach (var item in list)
207+
{
208+
var newItem = compiled(item);
209+
210+
foreach (var accessor in accessors)
211+
{
212+
var value = accessor.GetValue(newItem);
213+
accessor.SetValue(item, value);
214+
}
215+
}
216+
217+
return list.Count;
218+
}
219+
195220
var dbContext = query.GetDbContext();
196221

197222
#if EF6
@@ -994,6 +1019,11 @@ public List<Tuple<string, object>> GetInnerValues<T>(IQueryable<T> query, Expres
9941019
valueSql = valueSql.Replace("AS [C1]", "");
9951020
valueSql = valueSql.Replace("AS `C1`", "");
9961021

1022+
if (valueSql.Trim().StartsWith("TOP") && valueSql.IndexOf(")") != -1)
1023+
{
1024+
valueSql = valueSql.Substring(valueSql.IndexOf(")") + 1);
1025+
}
1026+
9971027
var listReplace = new List<string>()
9981028
{
9991029
"[Extent1]",

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ public class BatchUpdateManager
2525
/// <summary>Gets or sets the factory to create an InMemory DbContext.</summary>
2626
/// <value>The factory to create an InMemory DbContext.</value>
2727
public static Func<DbContext> InMemoryDbContextFactory { get; set; }
28+
#else
29+
/// <summary>
30+
/// Gets or sets a value indicating whether this object is in memory query.
31+
/// </summary>
32+
/// <value>True if this object is in memory query, false if not.</value>
33+
public static bool IsInMemoryQuery { get; set; }
2834
#endif
2935
}
3036
}

0 commit comments

Comments
 (0)