Skip to content

ToQueryString parameter values with float (and maybe real?) are wrong #27697

@ErikEJ

Description

@ErikEJ

It is important that these values are correct if you rely on ToQueryString (DebugView) as a troubleshooting tool.

CREATE TABLE[dbo].[FloatTable]
(      
     [Id] INT  IDENTITY   NOT NULL,  
     [Data] FLOAT NULL
 );
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using System;
using System.Linq;

namespace ConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            using var nw = new NWContext();

            // Alter this value for each run
            double? testval = 4.5655d;

            nw.FloatTables.Add(new FloatTable
            {
                Data = testval,
            });

            nw.SaveChanges();

            var query = nw.FloatTables.Where(x => x.Data == testval).ToQueryString();

            var found = nw.FloatTables.SingleOrDefault(x => x.Data == testval);
        }
    }

    public partial class FloatTable
    {
        public int Id { get; set; }
        public double? Data { get; set; }
    }

    public partial class NWContext : DbContext
    {
        public NWContext()
        {
        }

        public virtual DbSet<FloatTable> FloatTables { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
                optionsBuilder.UseSqlServer("Data Source=.\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True", x => x.UseNetTopologySuite())
                    .LogTo(Console.WriteLine)
                    .EnableSensitiveDataLogging();
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.ApplyConfiguration(new FloatTableConfiguration());
        }
    }

    public partial class FloatTableConfiguration : IEntityTypeConfiguration<FloatTable>
    {
        public void Configure(EntityTypeBuilder<FloatTable> entity)
        {
            entity.ToTable("FloatTable");
        }
    }
}

ToQueryString:

DECLARE @__testval_0 float = 4.5654000000000003E0;

SELECT[f].[Id], [f].[Data]
        FROM[FloatTable] AS[f]
WHERE[f].[Data] = @__testval_0

EF Core log:

Executing DbCommand[Parameters =[@__testval_0 = '4.5654'(Nullable = true)], CommandType = 'Text', CommandTimeout = '30']

SELECT TOP(2) [f].[Id], [f].[Data]
        FROM[FloatTable] AS[f]
WHERE[f].[Data] = @__testval_0

EF Core version: 6.0.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 6.0

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions