When EnablePlanCaching is true, executing two queries in sequence when the only difference between the queries is between DateTime/DateTimeOffset constant value (in the milliseconds range), the second query always returns the same result as the first query.
As a result, incorrect data might be read.
The issue seems to be in ExpressionKeyGen class, where comparison is done via string representations of the expressions, where milliseconds values are clipped (ie precision is captured including seconds but not milliseconds.)
This seems to be related to the precision of DateTime and DateTimeOffset, but we should investigate similar issues with other primitive types as well.
Workarounds:
- Disabling caching on the query, easier in the ObjectContext API than on DbContext.
- Parameterize all the constants of the types affected by assigning the values to variable, and referencing the variable in the query, which should prevent the value to be copied into the query key as a string
As a result, incorrect data might be read.
The issue seems to be in ExpressionKeyGen class, where comparison is done via string representations of the expressions, where milliseconds values are clipped (ie precision is captured including seconds but not milliseconds.)
This seems to be related to the precision of DateTime and DateTimeOffset, but we should investigate similar issues with other primitive types as well.
Workarounds:
- Disabling caching on the query, easier in the ObjectContext API than on DbContext.
- Parameterize all the constants of the types affected by assigning the values to variable, and referencing the variable in the query, which should prevent the value to be copied into the query key as a string