borisdj / EFCore.BulkExtensions

Entity Framework EF Core efcore Bulk Batch Extensions with BulkCopy in .Net for Insert Update Delete Read (CRUD), Truncate and SaveChanges operations on SQL Server, PostgreSQL, MySQL, SQLite
https://codis.tech/efcorebulk
Other
3.61k stars 578 forks source link

BulkExtensions determines target DbContext incorrectly when calls to different database servers in parallel #1491

Open micaelle opened 3 months ago

micaelle commented 3 months ago

I have a test code which calls some Bulk-method (BulkInsertOrUpdateAsync for instance). And when I launching this test in parallel for different db servers (MsSql and PostgreSql) it send queries to wrong targets - PostgreSql-queries to the MsSql server or, vice versa, MsSql-queries to Postgres.

This looks like issue #964, the error message is the same.

The root cause is database type have determined in static class (SqlAdaptersMapping.DbServer) and this works incorrect in parallel execution.

Async lock around of Bulk* calls was the solution for me. But seems this behavior may lead to complex bugs in multi-server configurations. Probably will valuable to mention that in readme, at least?

b4icurmt commented 3 months ago

I encounter the same situation. I have different tests running using different kinds of DB servers. The error I get is System.InvalidCastException : Unable to cast object of type 'Microsoft.Data.Sqlite.SqliteConnection' to type 'Microsoft.Data.SqlClient.SqlConnection'.