Efcore: Exception when applying Count() or CountAsync() to IQueriable<T>

Created on 14 Aug 2019  路  8Comments  路  Source: dotnet/efcore

solution.zip

I have recently upgraded EntityFramework.Core 3.0 Nuget packages to the latest preview release (3.0.0-preview8.19405.11)

After the update, the same code that used to work started to fail with the following exception when I try to get a LINQ CountAsync() or Count() out of an IQueriable:

System.InvalidCastException: 'Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.SqlExpressions.SqlFunctionExpression' to type 'System.Linq.Expressions.ConstantExpression'.'

Before the nuget upgrade, same code worked fine. Note that ToList() and ToListAsync() both work fine (if you try to put them there), just the Count() and CountAsync() are failing.
```

Steps to reproduce

Unzip the attached solution.zip and run the Test project. You should get the exception I described above.

Further technical details

EF Core version: 3.0.0-preview8.19405.11
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10
IDE: Visual Studio 2019 16.3.0 Preview 2

closed-duplicate customer-reported

Most helpful comment

It already works in current nightly build. So may be different issue but already fixed.

All 8 comments

The version I was using before (that worked fine) is 4.6.0-preview7.19362.9

Note for triage: confirmed this works with latest daily build.

Duplicate of #16722

I don't understand how this cannot be evaluated on SQL Server side. Here is a code that fails:

using (var context = new SqlServerDataContext())
{
var count = await context.Set().CountAsync();
}

I believe that this might be a different issue.

It already works in current nightly build. So may be different issue but already fixed.

Thank you!

Just wanted to let you know that the problem is still there in Preview 8 (3.0.0-preview8.19405.11). Did the fix not get included in the release?

@ibasin It's unlikely that the fix made it to preview 8. Preview 8 is quite out-of-date now; the daily builds are your best bet.

Was this page helpful?
0 / 5 - 0 ratings