I have a scenario where I have a non-nullable column in my table but a my POCO model is nullable. (I know, not ideal. It's a brownfield and I'm trying to avoid a projection). I created a converter as follows:
class NullDecimalConverter : ValueConverter<decimal?, double>
{
public NullDecimalConverter() : base(
model => model == null ? 0 : (double)model,
dbValue => dbValue == 0 ? null : (decimal)dbValue,
convertsNulls: true
)
{ }
}
info: 2/27/2024 15:35:42.582 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command)
Executed DbCommand (31ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
SELECT [p].[order_id], [p].[compliance_check], [p].[quantity]
FROM [proposed_orders] AS [p]
WHERE [p].[quantity] IS NULL
Ultimately, we'll probably end up solving it at the database level so no converters are necessary, but it struck me as odd that EF got it right on the first pass but didn't actually use that SQL.
Include provider and version information
EF Core version: 8.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 8.0
Operating system: Windows 11
IDE: Visual Studio 2022 17.9)
I have a scenario where I have a non-nullable column in my table but a my POCO model is nullable. (I know, not ideal. It's a brownfield and I'm trying to avoid a projection). I created a converter as follows:
Then mapped as follows:
When use the following Linq query...
It generates the correct SQL at the start of the process, but the incorrect SQL to execute:
The select statement compiled here is correct...
But then this is what it finally executed...
Ultimately, we'll probably end up solving it at the database level so no converters are necessary, but it struck me as odd that EF got it right on the first pass but didn't actually use that SQL.
Include provider and version information
EF Core version: 8.0 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 8.0 Operating system: Windows 11 IDE: Visual Studio 2022 17.9)