Skip to content

Commit df8e21f

Browse files
author
zzzprojects
committed
Update to v1.3.4
QueryCache: Add ConnecitonString in Cache Key QueryIncludeOptimized: Add AllowQueryBatch property
1 parent 9130870 commit df8e21f

File tree

75 files changed

+2238
-87
lines changed

Some content is hidden

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

75 files changed

+2238
-87
lines changed

src/Z.EntityFramework.Plus.EF5.NET40/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("e4c2af73-caeb-4429-bcb6-0a359484e064")]
21-
[assembly: AssemblyVersion("1.2.4")]
22-
[assembly: AssemblyFileVersion("1.2.4")]
21+
[assembly: AssemblyVersion("1.3.4")]
22+
[assembly: AssemblyFileVersion("1.3.4")]

src/Z.EntityFramework.Plus.EF5.NET40/QueryCache/QueryCacheManager.cs

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
using System.Collections.Concurrent;
99
using System.Collections.Generic;
10+
using System.Data.Common;
1011
using System.Linq;
1112
using System.Text;
12-
1313
#if EF5 || EF6
1414
using System.Runtime.Caching;
1515

@@ -48,8 +48,8 @@ static QueryCacheManager()
4848
/// <value>The default cache item policy to use when no policy is specified.</value>
4949
public static CacheItemPolicy DefaultCacheItemPolicy { get; set; }
5050
#elif EFCORE
51-
/// <summary>Gets or sets the cache to use for the QueryCacheExtensions extension methods.</summary>
52-
/// <value>The cache to use for the QueryCacheExtensions extension methods.</value>
51+
/// <summary>Gets or sets the cache to use for the QueryCacheExtensions extension methods.</summary>
52+
/// <value>The cache to use for the QueryCacheExtensions extension methods.</value>
5353
public static IMemoryCache Cache { get; set; }
5454

5555
/// <summary>Gets or sets the default memory cache entry options to use when no policy is specified.</summary>
@@ -72,7 +72,7 @@ internal static void AddCacheTag(string cacheKey, params string[] tags)
7272
{
7373
foreach (var tag in tags)
7474
{
75-
CacheTags.AddOrUpdate(tag, x => new List<string> { cacheKey }, (x, list) =>
75+
CacheTags.AddOrUpdate(tag, x => new List<string> {cacheKey}, (x, list) =>
7676
{
7777
if (!list.Contains(x))
7878
{
@@ -112,10 +112,11 @@ public static string GetCacheKey(IQueryable query, string[] tags)
112112
{
113113
var sb = new StringBuilder();
114114

115-
#if EF5 || EF6
115+
#if EF5
116116
var objectQuery = query.GetObjectQuery();
117117

118118
sb.AppendLine(CachePrefix);
119+
sb.AppendLine(objectQuery.Context.Connection.ConnectionString);
119120
sb.AppendLine(string.Join(";", tags));
120121
sb.AppendLine(objectQuery.ToTraceString());
121122

@@ -126,12 +127,29 @@ public static string GetCacheKey(IQueryable query, string[] tags)
126127
sb.Append(parameter.Value);
127128
sb.AppendLine(";");
128129
}
129-
#elif EFCORE
130+
#elif EF6
131+
var objectQuery = query.GetObjectQuery();
132+
133+
sb.AppendLine(CachePrefix);
134+
sb.AppendLine(objectQuery.Context.Connection.ConnectionString);
135+
sb.AppendLine(string.Join(";", tags));
130136

137+
var commandTextAndParameters = objectQuery.GetCommandTextAndParameters();
138+
sb.AppendLine(commandTextAndParameters.Item1);
139+
140+
foreach (DbParameter parameter in commandTextAndParameters.Item2)
141+
{
142+
sb.Append(parameter.ParameterName);
143+
sb.Append(";");
144+
sb.Append(parameter.Value);
145+
sb.AppendLine(";");
146+
}
147+
#elif EFCORE
131148
RelationalQueryContext queryContext;
132149
var command = query.CreateCommand(out queryContext);
133150

134151
sb.AppendLine(CachePrefix);
152+
sb.AppendLine(queryContext.Connection.ConnectionString);
135153
sb.AppendLine(string.Join(";", tags));
136154
sb.AppendLine(command.CommandText);
137155

@@ -156,10 +174,11 @@ public static string GetCacheKey<T>(QueryDeferred<T> query, string[] tags)
156174
{
157175
var sb = new StringBuilder();
158176

159-
#if EF5 || EF6
177+
#if EF5
160178
var objectQuery = query.Query.GetObjectQuery();
161179

162180
sb.AppendLine(CachePrefix);
181+
sb.AppendLine(objectQuery.Context.Connection.ConnectionString);
163182
sb.AppendLine(string.Join(";", tags));
164183
sb.AppendLine(objectQuery.ToTraceString());
165184

@@ -170,11 +189,29 @@ public static string GetCacheKey<T>(QueryDeferred<T> query, string[] tags)
170189
sb.Append(parameter.Value);
171190
sb.AppendLine(";");
172191
}
192+
#elif EF6
193+
var objectQuery = query.Query.GetObjectQuery();
194+
195+
sb.AppendLine(CachePrefix);
196+
sb.AppendLine(objectQuery.Context.Connection.ConnectionString);
197+
sb.AppendLine(string.Join(";", tags));
198+
199+
var commandTextAndParameters = objectQuery.GetCommandTextAndParameters();
200+
sb.AppendLine(commandTextAndParameters.Item1);
201+
202+
foreach (DbParameter parameter in commandTextAndParameters.Item2)
203+
{
204+
sb.Append(parameter.ParameterName);
205+
sb.Append(";");
206+
sb.Append(parameter.Value);
207+
sb.AppendLine(";");
208+
}
173209
#elif EFCORE
174210
RelationalQueryContext queryContext;
175211
var command = query.Query.CreateCommand(out queryContext);
176212

177213
sb.AppendLine(CachePrefix);
214+
sb.AppendLine(queryContext.Connection.ConnectionString);
178215
sb.AppendLine(string.Join(";", tags));
179216
sb.AppendLine(command.CommandText);
180217

src/Z.EntityFramework.Plus.EF5.NET40/QueryIncludeOptimized/QueryIncludeOptimizedChild`2.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,27 @@ public override void CreateIncludeQuery(IQueryable rootQuery)
5050
throw new Exception(ExceptionMessage.GeneralException);
5151
}
5252

53-
if (Filter != null)
53+
if (QueryIncludeOptimizedManager.AllowQueryBatch)
5454
{
55-
queryable.Select(Filter).Future();
55+
if (Filter != null)
56+
{
57+
queryable.Select(Filter).Future();
58+
}
59+
else
60+
{
61+
queryable.Select(FilterSingle).Future();
62+
}
5663
}
5764
else
5865
{
59-
queryable.Select(FilterSingle).Future();
66+
if (Filter != null)
67+
{
68+
var list = queryable.Select(Filter).ToList();
69+
}
70+
else
71+
{
72+
var list = queryable.Select(FilterSingle).ToList();
73+
}
6074
}
6175
}
6276
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
namespace Z.EntityFramework.Plus
9+
{
10+
public class QueryIncludeOptimizedManager
11+
{
12+
static QueryIncludeOptimizedManager()
13+
{
14+
AllowQueryBatch = true;
15+
}
16+
17+
public static bool AllowQueryBatch { get; set; }
18+
}
19+
}

src/Z.EntityFramework.Plus.EF5.NET40/QueryIncludeOptimized/QueryIncludeOptimizedParentQueryable`.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public IEnumerable<T> CreateEnumerable()
125125
}
126126

127127
// RESOLVE current and all future child queries
128-
return newQuery.Future().ToList();
128+
return QueryIncludeOptimizedManager.AllowQueryBatch ? newQuery.Future().ToList() : newQuery.ToList();
129129
}
130130

131131
/// <summary>Creates the queryable.</summary>

src/Z.EntityFramework.Plus.EF5.NET40/QueryIncludeOptimized/QueryIncludeOptimizedProvider.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
using System;
99
using System.Collections.Generic;
10+
using System.Data.Entity;
1011
using System.Linq.Expressions;
1112
using System.Reflection;
1213
using System.Threading;
@@ -191,10 +192,18 @@ public TResult Execute<TResult>(Expression expression)
191192
expressionProperty.SetValue(immediateQuery, expression);
192193
#endif
193194

194-
// EXECUTE the new expression
195-
var futureValue = immediateQuery.FutureValue();
195+
object value = null;
196196

197-
var value = futureValue.Value;
197+
if (QueryIncludeOptimizedManager.AllowQueryBatch)
198+
{
199+
value = immediateQuery.FutureValue().Value;
200+
}
201+
else
202+
{
203+
value = immediateQuery.Execute(objectQuery.MergeOption).FirstOrDefault();
204+
}
205+
// EXECUTE the new expression
206+
//var value = QueryIncludeOptimizedManager.AllowQueryBatch ? immediateQuery.FutureValue().Value : immediateQuery.FirstOrDefault();
198207

199208
// CHECK if a value has been returned
200209
if (value == null)

src/Z.EntityFramework.Plus.EF5.NET40/Z.EntityFramework.Plus.EF5.NET40.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
<Compile Include="QueryIncludeOptimized\BaseQueryIncludeOptimizedChild.cs" />
140140
<Compile Include="QueryIncludeOptimized\Extensions\IQueryable`.IncludeOptimized.cs" />
141141
<Compile Include="QueryIncludeOptimized\QueryIncludeOptimizedChild`2.cs" />
142+
<Compile Include="QueryIncludeOptimized\QueryIncludeOptimizedManager.cs" />
142143
<Compile Include="QueryIncludeOptimized\QueryIncludeOptimizedParentQueryable`.cs" />
143144
<Compile Include="QueryIncludeOptimized\QueryIncludeOptimizedProvider.cs" />
144145
<Compile Include="_Internal\Dictionary\Dictionary.AddOrAppend.cs" />
@@ -160,6 +161,7 @@
160161
<Compile Include="_Internal\EF5_EF6\Object\Object.GetObjectQuery.cs" />
161162
<Compile Include="_Internal\EF6\ObjectContext\GetDbContext.cs" />
162163
<Compile Include="_Internal\EF6\ObjectContext\GetInterceptionContext.cs" />
164+
<Compile Include="_Internal\EF6\ObjectQuery\GetCommandTextAndParameters.cs" />
163165
<Compile Include="_Internal\EFCore\CreateEntity\CreateEntityCommand.cs" />
164166
<Compile Include="_Internal\EFCore\CreateEntity\CreateEntityConnection.cs" />
165167
<Compile Include="_Internal\EFCore\CreateEntity\CreateEntityDataReader.cs" />
@@ -170,6 +172,7 @@
170172
<Compile Include="_Internal\EFCore\IQueryable`\CreateCommand.cs" />
171173
<Compile Include="_Internal\EFCore\IQueryable`\GetDbContext.cs" />
172174
<Compile Include="_Internal\EFCore\IQueryable`\IQueryable`.GetCommand.cs" />
175+
<Compile Include="_Internal\EFCore\IQueryable`\IsInMemoryQueryContext.cs" />
173176
<Compile Include="_Internal\EF\DbContext\DbContext.GetDbSetProperties.cs" />
174177
<Compile Include="_Internal\EF\DbContext\DbContext.MapReader.cs" />
175178
<Compile Include="_Internal\EF\IQueryable`\IQueryable.Order.cs" />

src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF/DbContext/DbContext.GetDbSetProperties.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ internal static List<PropertyInfo> GetDbSetProperties(this DbContext context)
3737
var isDbSet = setType.IsGenericType && (typeof (IDbSet<>).IsAssignableFrom(setType.GetGenericTypeDefinition()) || setType.GetInterface(typeof (IDbSet<>).FullName) != null);
3838
#elif EFCORE
3939
#if NETSTANDARD1_3
40-
var isDbSet = setType.GetType().IsConstructedGenericType && (typeof (DbSet<>).IsAssignableFrom(setType.GetGenericTypeDefinition()));
40+
var isDbSet = setType.GetTypeInfo().IsGenericType && (typeof (DbSet<>).IsAssignableFrom(setType.GetGenericTypeDefinition()));
4141
#else
4242
var isDbSet = setType.IsGenericType && (typeof(DbSet<>).IsAssignableFrom(setType.GetGenericTypeDefinition()));
4343
#endif

src/Z.EntityFramework.Plus.EF5.NET40/_Internal/EF5_EF6/Database/GetEntityConnection.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// More projects: http://www.zzzprojects.com/
66
// Copyright © ZZZ Projects Inc. 2014 - 2016. All rights reserved.
77

8-
#if FULL || BATCHDELETE || BATCHUPDATE
8+
#if FULL || BATCH_DELETE || BATCH_UPDATE
99
#if EF5 || EF6
1010

1111
#if EF5
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 || QUERY_CACHE
9+
#if EF6
10+
using System;
11+
using System.Data.Common;
12+
using System.Data.Entity.Core.EntityClient;
13+
using System.Data.Entity.Core.Objects;
14+
using System.Reflection;
15+
16+
namespace Z.EntityFramework.Plus
17+
{
18+
internal static partial class InternalExtensions
19+
{
20+
public static Tuple<string, DbParameterCollection> GetCommandTextAndParameters(this ObjectQuery objectQuery)
21+
{
22+
var stateField = objectQuery.GetType().BaseType.GetField("_state", BindingFlags.NonPublic | BindingFlags.Instance);
23+
var state = stateField.GetValue(objectQuery);
24+
var getExecutionPlanMethod = state.GetType().GetMethod("GetExecutionPlan", BindingFlags.NonPublic | BindingFlags.Instance);
25+
var getExecutionPlan = getExecutionPlanMethod.Invoke(state, new object[] {null});
26+
var prepareEntityCommandMethod = getExecutionPlan.GetType().GetMethod("PrepareEntityCommand", BindingFlags.NonPublic | BindingFlags.Instance);
27+
28+
var sql = "";
29+
using (var entityCommand = (EntityCommand) prepareEntityCommandMethod.Invoke(getExecutionPlan, new object[] {objectQuery.Context, objectQuery.Parameters}))
30+
{
31+
var getCommandDefinitionMethod = entityCommand.GetType().GetMethod("GetCommandDefinition", BindingFlags.NonPublic | BindingFlags.Instance);
32+
var getCommandDefinition = getCommandDefinitionMethod.Invoke(entityCommand, new object[0]);
33+
34+
var prepareEntityCommandBeforeExecutionMethod = getCommandDefinition.GetType().GetMethod("PrepareEntityCommandBeforeExecution", BindingFlags.NonPublic | BindingFlags.Instance);
35+
var prepareEntityCommandBeforeExecution = (DbCommand) prepareEntityCommandBeforeExecutionMethod.Invoke(getCommandDefinition, new object[] {entityCommand});
36+
37+
sql = prepareEntityCommandBeforeExecution.CommandText;
38+
var parameters = prepareEntityCommandBeforeExecution.Parameters;
39+
40+
return new Tuple<string, DbParameterCollection>(sql, parameters);
41+
}
42+
}
43+
}
44+
}
45+
46+
#endif
47+
#endif

0 commit comments

Comments
 (0)