-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Closed as duplicate of#15586
Closed as duplicate of#15586
Copy link
Description
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