question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:10 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
ajcvickerscommented, Mar 28, 2022

Thanks @ErikEJ. I’m guessing SqlClient is truncating the parameter. We will discuss.

0reactions
ErikEJcommented, Sep 13, 2022

@ajcvickers Great!

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Incorrect float data is inserted
In my SQL Server i have a table with a column called FloatValue With a Float data type. I update this value from...
Read more >
ToQueryString() "optimizes" the Where clause in an ...
When calling ToQueryString() with a parameter that is known to be invalid (for example, null on a required column), that Where clause is ......
Read more >
Strict configuration · Issue #1098 · pydantic ...
In #284, the reason given for not including a strict mode config was that pydantic would no longer be able to just call...
Read more >
SolrParams (Solr 8.9.0 API)
SolrParams is designed to hold parameters to Solr, often from the request ... Returns the Float value of the param, or null if...
Read more >
ModifiableSolrParams (Solr 8.0.0 API)
Returns an Iterator over the parameter names. String[], getParams(String param). returns an array of the String values of a param, or ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found