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

CAST in generated SQL statement leads to possible overflow in SQL or .NET #23602

Closed
klinckag opened this issue Dec 7, 2020 · 2 comments
Closed

Comments

@klinckag
Copy link

klinckag commented Dec 7, 2020

source code:
EFCoreCASTing.zip

Linq query to an SQL database table where Quantity is an integer and ItemPrice is a decimal(18,12)

                var q = from di in context.DeclaredItems
                        group di by 1 into g
                        select new
                        {
                            Price = g.Sum(e => e.Quantity * e.ItemPrice)
                        };

results in a CAST in the generated SQL

SUM(CAST([d].[Quantity] AS decimal(18,12)) * [d].[ItemPrice])

This results in overflow in SQL when Quantity >= 1000000

Because the result in SQL has a precision of 24 (where the last 12 digits
are always zero) we risk unnecessary overflow in .NET.
1000 * 100.000000000001 => 100000,000000001000000000000000

EF Core version: 3.1.9
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 3.1
Operating system: Windows 10
IDE: Microsoft Visual Studio Enterprise 2019 Version 16.7.7

@ajcvickers
Copy link
Member

@smitpatel I can't find the dupe...

@smitpatel
Copy link
Contributor

Related #15586
Duplicate of #14719

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

3 participants