Skip to content

System.InvalidOperationException: 'Unhandled expression '[Microsoft.EntityFrameworkCore.Query.SqlExpressions.SqlConstantExpression]' #37300

@janossuta

Description

@janossuta

Bug description

After upgrading from EF Core 8 to 10, one of our queries began throwing a System.InvalidOperationException.

This query is working in EF Core 8.

System.InvalidOperationException: 'Unhandled expression '[Microsoft.EntityFrameworkCore.Query.SqlExpressions.SqlConstantExpression]' of type 'Microsoft.EntityFrameworkCore.Query.SqlExpressions.SqlConstantExpression' encountered in 'SqlNullabilityProcessor'.'

This is a simplified version of the original query.

var factExchangeRatesMonthliesToDaily = await (from d in _context.DimDates
                                            join ere in _context.FactExchangeRatesMonthlies
                                                     on new { MonthId = (d.Year ?? 0) * 100 + (d.MonthNumber ?? 0) }
                                                 equals new { MonthId = ere.DimMonthId } into ere_1
                                            from ere in ere_1.DefaultIfEmpty()
                                            where filter.FromDate.GetYearMonthDayInt() <= d.DimDateId
                                               && filter.ToDate.GetYearMonthDayInt() >= d.DimDateId
                                               && ere.DimNumeratorCurrencyId == baseCurrency
                                               && ere.DimDenumeratorCurrencyId == newCurrency
                                            select new
                                            {
                                                d.DimDateId,
                                                ExchangeRate = ere != null ? ere.ExchangeRate : (decimal?)null,
                                                ere.DimDenumeratorCurrencyId,
                                                ere.DimNumeratorCurrencyId
                                            }).ToListAsync(); 

d.Year and d.MonthNumber fields are nullable

This is the part that fails in the new version:
on new { MonthId = (d.Year ?? 0) * 100 + (d.MonthNumber ?? 0) }

I resolved the issue by reorganizing the original query and removing the complex join condition. However, I'd like to know if this is a bug or if there's a way to fix it without modifying the query.

my repo with a runnable example

Your code

using DataLayer.Models;
using KinectOnline.Services.Extensions;
using Microsoft.EntityFrameworkCore;

var webmartContextOptions = new DbContextOptionsBuilder<WEBMartContext>()
        .UseSqlServer("Server=localhost,1433;Database=WEBMart;Integrated Security=True;TrustServerCertificate=True")
        .Options;

using var context = new WEBMartContext(webmartContextOptions);

await context.Database.EnsureCreatedAsync();

await GetVolumeAndSpotcostProductionAsync(context);

async Task GetVolumeAndSpotcostProductionAsync(WEBMartContext _context)
{
    var filter = new 
    {
        FromDate = new DateTime(2025, 1, 1),
        ToDate = new DateTime(2025, 1, 31) 
    };
  
    string currency = "NOK";
    string baseCurrency = "EUR";

    string newCurrency = currency ?? baseCurrency;


    var factExchangeRatesMonthliesToDaily = await (from d in _context.DimDates
                                            join ere in _context.FactExchangeRatesMonthlies
                                                     on new { MonthId = (d.Year ?? 0) * 100 + (d.MonthNumber ?? 0) }
                                                 equals new { MonthId = ere.DimMonthId } into ere_1
                                            from ere in ere_1.DefaultIfEmpty()
                                            where filter.FromDate.GetYearMonthDayInt() <= d.DimDateId
                                               && filter.ToDate.GetYearMonthDayInt() >= d.DimDateId
                                               && ere.DimNumeratorCurrencyId == baseCurrency
                                               && ere.DimDenumeratorCurrencyId == newCurrency
                                            select new
                                            {
                                                d.DimDateId,
                                                ExchangeRate = ere != null ? ere.ExchangeRate : (decimal?)null,
                                                ere.DimDenumeratorCurrencyId,
                                                ere.DimNumeratorCurrencyId
                                            }).ToListAsync();
}

Stack traces


Verbose output


EF Core version

10.0.0

Database provider

No response

Target framework

.NET 10.0

Operating system

No response

IDE

No response

Metadata

Metadata

Assignees

Type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions