11// Licensed to the .NET Foundation under one or more agreements.
22// The .NET Foundation licenses this file to you under the MIT license.
33
4+ using System . Collections ;
45using System . Collections . Concurrent ;
56using Microsoft . Extensions . Caching . Memory ;
67
@@ -104,21 +105,34 @@ void IPrintableExpression.Print(ExpressionPrinter expressionPrinter)
104105
105106 private readonly struct CommandCacheKey : IEquatable < CommandCacheKey >
106107 {
108+ private static readonly bool UseOldBehavior34201 =
109+ AppContext . TryGetSwitch ( "Microsoft.EntityFrameworkCore.Issue34028" , out var enabled34028 ) && enabled34028 ;
110+
107111 private readonly Expression _queryExpression ;
108112 private readonly Dictionary < string , ParameterInfo > _parameterInfos ;
109113
110- internal CommandCacheKey ( Expression queryExpression , IReadOnlyDictionary < string , object ? > parameterValues )
114+ // Quirk only
115+ private readonly IReadOnlyDictionary < string , object ? > ? _parameterValues ;
116+
117+ public CommandCacheKey ( Expression queryExpression , IReadOnlyDictionary < string , object ? > parameterValues )
111118 {
112119 _queryExpression = queryExpression ;
113120 _parameterInfos = new Dictionary < string , ParameterInfo > ( ) ;
114121
115- foreach ( var ( key , value ) in parameterValues )
122+ if ( UseOldBehavior34201 )
116123 {
117- _parameterInfos [ key ] = new ParameterInfo
124+ _parameterValues = parameterValues ;
125+ }
126+ else
127+ {
128+ foreach ( var ( key , value ) in parameterValues )
118129 {
119- IsNull = value is null ,
120- ObjectArrayLength = value is object [ ] arr ? arr . Length : null
121- } ;
130+ _parameterInfos [ key ] = new ParameterInfo
131+ {
132+ IsNull = value is null ,
133+ ObjectArrayLength = value is object [ ] arr ? arr . Length : null
134+ } ;
135+ }
122136 }
123137 }
124138
@@ -140,11 +154,37 @@ public bool Equals(CommandCacheKey commandCacheKey)
140154
141155 if ( _parameterInfos . Count > 0 )
142156 {
143- foreach ( var ( key , info ) in _parameterInfos )
157+ if ( UseOldBehavior34201 )
158+ {
159+ foreach ( var ( key , value ) in _parameterValues ! )
160+ {
161+ if ( ! _parameterValues . TryGetValue ( key , out var otherValue ) )
162+ {
163+ return false ;
164+ }
165+
166+ // ReSharper disable once ArrangeRedundantParentheses
167+ if ( ( value == null ) != ( otherValue == null ) )
168+ {
169+ return false ;
170+ }
171+
172+ if ( value is IEnumerable
173+ && value . GetType ( ) == typeof ( object [ ] ) )
174+ {
175+ // FromSql parameters must have the same number of elements
176+ return ( ( object [ ] ) value ) . Length == ( otherValue as object [ ] ) ? . Length ;
177+ }
178+ }
179+ }
180+ else
144181 {
145- if ( ! commandCacheKey . _parameterInfos . TryGetValue ( key , out var otherInfo ) || info != otherInfo )
182+ foreach ( var ( key , info ) in _parameterInfos )
146183 {
147- return false ;
184+ if ( ! commandCacheKey . _parameterInfos . TryGetValue ( key , out var otherInfo ) || info != otherInfo )
185+ {
186+ return false ;
187+ }
148188 }
149189 }
150190 }
0 commit comments