Pomelo.EntityFrameworkCore.MySql: Unparameterized .Contains query using incorrect values after periods of high traffic
Steps to reproduce
Unable to reproduce at this time
The issue
I have an API that switches DbContext from request to request based on the customer accessing it (this is because we use single tenant databases). We are seeing on occasion, after periods of high traffic, that the expressions compiled after using the Contains()
operator use old values, as though a cache is being exhausted.
To give you some context:
Users might call GET /myEndpoint?ownerId=ABC&ownerId=DEF
The query parameters get translated into SQL via LINQ using something similar to:
if (query.OwnerIds!= null && query.OwnerIds.Any())
{
predicate = predicate.And(p => query.OwnerIds.Contains(p.OwnerId) || query.OwnerIds.Contains(p.OwnerId2) || query.OwnerIds.Contains(p.OwnerId3));
}
The predicate then gets used when calling the database context ie: dbContext.Data.Where(predicate)
This correctly gets converted to a MySQL statement using IN ()
as I’d expect. If we take the URL example above (GET /myEndpoint?ownerId=ABC&ownerId=DEF
) however, the query that gets executed against the database actually uses values from a previous query, so it might run WHERE ownerId IN ('GHI', 'JKL') OR ownerId2 IN ('GHI', 'JKL') OR ownerId3 IN ('GHI', 'JKL')
.
This only seems to occur after a period of high traffic has hit the API. Restarting the container that the API runs in resolves the issue, until it eventually happens again. The services run on docker containers on AWS ECS Fargate
I can rewrite the LINQ to chain OR conditions together, which then results in a parameterized query but rather horrible SQL. Can anybody suggest anything that might be causing the underlying problem? I’m not bothered if the IN ()
is not parameterized IF I can explain and resolve what’s going on with old values being used.
Further technical details
MySQL version: 5.7.12
Operating system: Windows 10
Pomelo.EntityFrameworkCore.MySql version: 5.0.0 5.0.0-alpha.2
Microsoft.AspNetCore.App version: 3.1
EntityFrameworkCore version: 5.0.1
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Comments: 18
I can confirm that this issue is a provider issue and not efcore itself and it seems to happen only in version : 5.0.0-alpha.2 and I was able to reproduce this in a simple console application not sure if it helps
efcore_mysql_bug.zip
Item also logged on main efcore project at https://github.com/dotnet/efcore/issues/26905