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 UInt64 ToString SQL generation #20433

Closed
aubiyko opened this issue Mar 27, 2020 · 0 comments · Fixed by #20470
Closed

Incorrect UInt64 ToString SQL generation #20433

aubiyko opened this issue Mar 27, 2020 · 0 comments · Fixed by #20470
Assignees
Labels
area-type-mapping closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Milestone

Comments

@aubiyko
Copy link

aubiyko commented Mar 27, 2020

LINQ query with UInt64.ToString() to MS SQL Server with lagre property value produces flawed SQL code leading to exception.

Steps to reproduce

Suppose simple entity:

    public class DemoEntity
    {
        [Key,
        DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }

        public Int64 LongProperty { get; set; }

        public UInt64 ULongProperty { get; set; }
    }

with simple DbContext using UseSqlServer().

Add-Migration results with LongProperty as bigint and ULongProperty as decimal(20, 0).

ModelSnapshot.BuildModel
modelBuilder
                .HasAnnotation("ProductVersion", "3.1.3")
                .HasAnnotation("Relational:MaxIdentifierLength", 128)
                .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

            modelBuilder.Entity("ForEFCoreGithubIssue.DemoEntity", b =>
                {
                    b.Property<int>("Id")
                        .ValueGeneratedOnAdd()
                        .HasColumnType("int")
                        .HasAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);

                    b.Property<long>("LongProperty")
                        .HasColumnType("bigint");

                    b.Property<decimal>("ULongProperty")
                        .HasColumnType("decimal(20,0)");

                    b.HasKey("Id");

                    b.ToTable("Data");
                });

Sample database has an least one row with LongProperty equals to Int64.MaxValue and ULongProperty equals to UInt64.MaxValue.

Then query

string needle = "1";
context.Data.Where(x => x.ULongProperty.ToString().Contains(needle))

produces

SELECT [d].[Id], [d].[LongProperty], [d].[ULongProperty]
FROM [Data] AS [d]
WHERE (@__needle_0 = N'') OR (CHARINDEX(@__needle_0, CONVERT(VARCHAR(19), [d].[ULongProperty])) > 0)

that leads to Microsoft.Data.SqlClient.SqlException "Arithmetic overflow error converting numeric to data type varchar.".

Note

Note that

string needle = "1";
context.Data.Where(x => x.LongProperty.ToString().Contains(needle))

produces

SELECT [d].[Id], [d].[LongProperty], [d].[ULongProperty]
FROM [Data] AS [d]
WHERE (@__needle_0 = N'') OR (CHARINDEX(@__needle_0, CONVERT(VARCHAR(20), [d].[LongProperty])) > 0)

that works fine.

The problem is that for ULongProperty generated SQL query assumes 19 character length string, which is too short. Manual row SQL query replaced with VARCHAR(20) works fine.

Further technical details

EF Core version: 3.1.3
Database provider: Microsoft.EntityFrameworkCore.SqlServer v.3.1.3
Target framework: .NET Core 3.1
Operating system: MS Windows 10 1909
IDE: MS VS Community 2019 16.5.1
DB: MS SQL LocalDB 14.0.1000.169

@ajcvickers ajcvickers added this to the 5.0.0 milestone Mar 27, 2020
smitpatel added a commit that referenced this issue Mar 30, 2020
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Mar 30, 2020
smitpatel added a commit that referenced this issue Mar 31, 2020
smitpatel added a commit that referenced this issue Mar 31, 2020
smitpatel added a commit that referenced this issue Mar 31, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0, 5.0.0-preview3 Mar 31, 2020
@smitpatel smitpatel modified the milestones: 5.0.0-preview3, 5.0.0 Apr 1, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0, 5.0.0-preview4 Apr 20, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-preview4, 5.0.0 Nov 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-type-mapping closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants