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

Incorrect results when coalescing expressions of different types #35516

Closed
ranma42 opened this issue Jan 22, 2025 · 2 comments
Closed

Incorrect results when coalescing expressions of different types #35516

ranma42 opened this issue Jan 22, 2025 · 2 comments

Comments

@ranma42
Copy link
Contributor

ranma42 commented Jan 22, 2025

Bug description

The ?? operator can return unexpected results when applied to operands with a different type (mapping).

In the attached code, the expected result for the null ?? 2.25 case would be 2.25, but EFCore actually returns 2.

Your code

// @nuget: Microsoft.EntityFrameworkCore.Sqlite -Version 9.0.1

using System;
using System.Data;
using System.Linq;
using Microsoft.EntityFrameworkCore;

using var db = new BloggingContext();

db.Database.EnsureDeleted();
db.Database.EnsureCreated();

var result = db
	.MyEntities
	.Select(x => x.Value ?? 2.25)
	.ToList();

foreach (var item in result) {
	Console.WriteLine($"{item.GetType()} {item}");
}

public class BloggingContext : DbContext
{
    public DbSet<MyEntity> MyEntities { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder options)
        => options
            .LogTo(Console.WriteLine, Microsoft.Extensions.Logging.LogLevel.Information)
            .EnableSensitiveDataLogging()
            .UseSqlite("Data Source=test.db");

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<MyEntity>().HasData(new MyEntity { Id = 1, Value = 5 });
        modelBuilder.Entity<MyEntity>().HasData(new MyEntity { Id = 2 });
    }
}

public class MyEntity
{
    public int Id { get; set; }
    public int? Value { get; set; }
}

Stack traces


Verbose output

info: 01/22/2025 23:16:55.864 RelationalEventId.CommandExecuted[20101] (Microsoft.EntityFrameworkCore.Database.Command) 
      Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
      SELECT COALESCE("m"."Value", 2.25)
      FROM "MyEntities" AS "m"
System.Double 5
System.Double 2

EF Core version

9.0.1

Database provider

Microsoft.EntityFrameworkCore.Sqlite

Target framework

.NET 9.0

Operating system

Kali Linux

IDE

Visual Studio Code 1.96.4

@ranma42 ranma42 changed the title Incorrect results when coalescing expression of different types Incorrect results when coalescing expressions of different types Jan 22, 2025
@cincuranet
Copy link
Contributor

Related to #15586.

@maumar
Copy link
Contributor

maumar commented Feb 3, 2025

InferTypeMapping goes through all arguments and takes first non-null mapping as a result, so we take the type mapping from the left, which is int. Dupe of #15586

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

No branches or pull requests

4 participants