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

Specified cast not valid when using min or max after decimal to float cast #10537

Closed
mpdn opened this issue Dec 12, 2017 · 1 comment
Closed
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Milestone

Comments

@mpdn
Copy link

mpdn commented Dec 12, 2017

Casting from decimal to float before a Min() or Max() throws an exception with a specified cast not valid message and the following stack trace:

at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.GetResult[TResult](IEnumerable`1 valueBuffers, Boolean throwOnNullResult)
at lambda_method(Closure , QueryContext )
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass17_1`1.<CompileQueryCore>b__0(QueryContext qc)
at TestProject.TestProgram.Main() in C:\...\Program.cs:line 84

The bug can be recreated with the following code:

public class TestTable
{
    public int Id { get; set; }
    public decimal Thing { get; set; }
}

public class TestContext : DbContext
{
    public DbSet<TestTable> ThingTable { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder builder)
    {
        builder.UseSqlServer(@"Server=SomeServer;Database=SomeDatabase;Trusted_Connection=True");
    }
}

class TestProgram
{
    public static void Main()
    {
        using (var db = new TestContext())
        {
            db.Database.EnsureCreated();

            db.ThingTable.Add(new TestTable
            {
                Thing = 20
            });

            db.SaveChanges();

            Console.WriteLine(db.ThingTable.Select(x => (float)x.Thing).Min());
        }
    }
}

.Sum() works fine. Also .Skip(0).Min() seems to be a workaround, oddly enough.

Further technical details

EF Core version: 2.0.1
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2017 15.4

@ajcvickers ajcvickers added this to the 2.1.0 milestone Dec 13, 2017
@ajcvickers ajcvickers modified the milestones: 2.1.0-preview1, 2.1.0 Jan 17, 2018
maumar added a commit that referenced this issue Feb 14, 2018
…r decimal to float cast

Problem was that as part of fixing #8652 we were removing explicit casts when processing result operators. This is fine for most aggregate operators, but is not correct for Min/Max. This is because the final result type is the same as the argument - original type of the argument must be preserved or we risk type mismatches in the final result.

Additionally improved the fix for #1251 - we should be stripping order by clauses for Min/Max operators as well. Previously we exempt all ChoiceResultOperatorBase, instead of correct First/Single/Last.
maumar added a commit that referenced this issue Feb 14, 2018
…r decimal to float cast

Problem was that as part of fixing #8652 we were removing explicit casts when processing result operators. This is fine for most aggregate operators, but is not correct for Min/Max. This is because the final result type is the same as the argument - original type of the argument must be preserved or we risk type mismatches in the final result.

Additionally improved the fix for #1251 - we should be stripping order by clauses for Min/Max operators as well. Previously we exempt all ChoiceResultOperatorBase, instead of correct First/Single/Last.
@maumar
Copy link
Contributor

maumar commented Feb 14, 2018

fixed in fdb54d9

@maumar maumar closed this as completed Feb 14, 2018
@maumar maumar added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Feb 14, 2018
@ajcvickers ajcvickers modified the milestones: 2.1.0-preview2, 2.1.0 Nov 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. type-bug
Projects
None yet
Development

No branches or pull requests

3 participants