Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Regression in the translation of SqlConstantExpression #35355

Open
ranma42 opened this issue Dec 19, 2024 · 1 comment · Fixed by #35357
Open

Regression in the translation of SqlConstantExpression #35355

ranma42 opened this issue Dec 19, 2024 · 1 comment · Fixed by #35357

Comments

@ranma42
Copy link
Contributor

ranma42 commented Dec 19, 2024

In EFCore 9 the construction of SqlConstantExpression from a ConstantExpression has regressed.

// @nuget: Microsoft.EntityFrameworkCore.SqlServer -Version 9.0.0
using System;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Query.SqlExpressions;

using var db = new BloggingContext();

var query = db.Blogs
    .Select(x => db.Fail())
    .ToQueryString();

Console.WriteLine(query);

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options.UseSqlServer();

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder
            .HasDbFunction(typeof(BloggingContext).GetMethod(nameof(Fail))!)
            .HasTranslation(args => new SqlConstantExpression(Expression.Constant(true), null));
	}

    public bool Fail() => throw new NotSupportedException();
}

public class Blog
{
    public int Id { get; set; }
}

This snippet emits

SELECT NULL
FROM [Blogs] AS [b]

on EFCore 9 and

SELECT CAST(1 AS bit)
FROM [Blogs] AS [b]

on EFCore 8.

This regression seems to be caused by fbfd468#diff-31a49e080013a5417ff3104f9a840e3a5693f553c3ee840cdb7c8a4cb8e335f8R46-R50
Apparently the constructor has been marked as Obsolete, but it can still be used (which might be especially problematic if used indirectly, through a dependency).

@roji
Copy link
Member

roji commented Dec 19, 2024

Thanks @ranma42 🤦

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants