linq2db / linq2db.EntityFrameworkCore

Bring power of Linq To DB to Entity Framework Core projects
MIT License
462 stars 38 forks source link

SQLite: DateTime Length Fallback Of 0 Leads To Empty Column Value On Insert #349

Closed warappa closed 12 months ago

warappa commented 12 months ago

Issue

I have a SQLite database (for my integration tests). All the mapping is inferred from the EF Core mapping (no manual configuration/attributes).

When I use BulkCopy with BulkCopyOptions using UseParameters = true, a DateTime value gets written as an empty string to the SQLite database, although the DateTime value is set to a valid, non-default value.

Investigation

I debugged this and traced it back to this line https://github.com/linq2db/linq2db.EntityFrameworkCore/blob/44de54e460309d3790a8eba8fc3375276201c93f/Source/LinqToDB.EntityFrameworkCore/EFCoreMetadataReader.cs#L297

GetMaxLength() returns indeed null, fallback of 0 is used.

As far as I tested, only SQLite is affected (SQL Server and Oracle look to work fine).

Debugging

Used the debug symbols and debugged it. At the mentioned line I set the value of length to null instead of 0. This fixed the issue for me in this debug run.

Theory

It seems for SQLite, this fallback causes Linq2db to not send anything. I guess, because in SQLite there is no DateTime type but it is represented (in my case) as TEXT, it trims the "text" to a length/size of 0 (fallback value). This leads to an empty string.

Expected

DateTime column value should not be cut down to an empty string but have a full date-time value.

Workaround

None that worked. Using UseParameters = false in BulkCopyOptions works for SQLite, but is inefficient in the real provider (Oracle). I think in some way I can workaround it in the non-test implementation, but then the tests change the "unsuspecting" domain implementation which I dislike. So a fix would be appreciated.

Failed tries:

Info

.NET: 7.10 linq2db: 5.2.2 linq2db.EntityFramework: 7.5.0