DataTables.Queryable icon indicating copy to clipboard operation
DataTables.Queryable copied to clipboard

AutoMapper ProjectTo() blows up.

Open VictorioBerra opened this issue 6 years ago • 4 comments

System.InvalidCastException: Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[MyApp.API.ViewModels.InventoryServerViewModel]' to type 'System.Linq.IQueryable`1[MyApp.Entities.MyInventoryServer]'.
   at DataTables.Queryable.DataTablesQueryProvider`1.CreateQuery(Expression expression)
   at AutoMapper.QueryableExtensions.ProjectionExpression.Select(IQueryable source, LambdaExpression lambda) in C:\projects\automapper\src\AutoMapper\QueryableExtensions\ProjectionExpression.cs:line 89
   at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable`1 source, TAccumulate seed, Func`3 func)
   at AutoMapper.QueryableExtensions.ProjectionExpression.To[TResult](Object parameters, Expression`1[] membersToExpand) in C:\projects\automapper\src\AutoMapper\QueryableExtensions\ProjectionExpression.cs:line 62
   at AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable source, IConfigurationProvider configuration, Object parameters, Expression`1[] membersToExpand) in C:\projects\automapper\src\AutoMapper\QueryableExtensions\Extensions.cs:line 60
   at AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable source, IConfigurationProvider configuration, Expression`1[] membersToExpand) in C:\projects\automapper\src\AutoMapper\QueryableExtensions\Extensions.cs:line 76

http://docs.automapper.org/en/stable/Queryable-Extensions.html

https://github.com/AlexanderKrutov/DataTables.Queryable/blob/master/DataTables.Queryable/DataTablesQueryProvider.cs#L17-L25

My code:

            return queryable
               .Filter(request)
            .ProjectTo<TReturn>(mapperConfig)

I think ProjectTo is invoking CreateQuery because under the hood it runs .Select(). I am not sure, I am a little out of my experience zone. Maybe we need to be doing AsQueryable() instead of casing in the QueryProvider?

VictorioBerra avatar Oct 30 '18 16:10 VictorioBerra

Hmm... I'm using DataTables.Queryable with AutoMapper too, but never faced with the same issue. May be it's because of Microsoft.EntityFrameworkCore.Query. Need time to verify.

AlexanderKrutov avatar Nov 01 '18 18:11 AlexanderKrutov

Give me a minute and I'll get you a sample.

On Thu, Nov 1, 2018, 1:29 PM Alexander Krutov <[email protected] wrote:

Hmm... I'm using DataTables.Queryable with AutoMapper too, but never faced with the same issue. May be it's because of Microsoft.EntityFrameworkCore.Query. Need time to verify.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/AlexanderKrutov/DataTables.Queryable/issues/29#issuecomment-435136791, or mute the thread https://github.com/notifications/unsubscribe-auth/ACzG6zdi-g2v2cMoxNbqkHBBO1q22Ll8ks5uqz1ugaJpZM4YCWsy .

VictorioBerra avatar Nov 01 '18 18:11 VictorioBerra

@AlexanderKrutov here you go:

https://github.com/VictorioBerra/DataTables.Queryable.ProjectTo.Issue.29

The code that breaks is here: https://github.com/VictorioBerra/DataTables.Queryable.ProjectTo.Issue.29/blob/master/Program.cs#L90

To get started run these, it will automatically drop the SQLIte DB and migrate and seed on every run.

git clone https://github.com/VictorioBerra/DataTables.Queryable.ProjectTo.Issue.29.git
cd DataTables-ProjectTo-issue-29/
dotnet run
Unhandled Exception: System.InvalidCastException: Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1[EFCore.Models.CatViewModel]' to type 'System.Linq.IQueryable`1[EFCore.Models.Cat]'.
   at DataTables.Queryable.DataTablesQueryProvider`1.CreateQuery(Expression expression)
   at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable`1 source, TAccumulate seed, Func`3 func)
   at AutoMapper.QueryableExtensions.ProjectionExpression.To[TResult](Object parameters, Expression`1[] membersToExpand) in C:\projects\automapper\src\AutoMapper\QueryableExtensions\ProjectionExpression.cs:line 62
   at AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable source, IConfigurationProvider configuration, Expression`1[] membersToExpand) in C:\projects\automapper\src\AutoMapper\QueryableExtensions\Extensions.cs:line 76
   at EFCore.Program.Main(String[] args) in C:\Users\me\Desktop\DataTables.Queryable.ProjectTo.Issue.29\Program.cs:line 90
   at EFCore.Program.<Main>(String[] args)

VictorioBerra avatar Nov 01 '18 19:11 VictorioBerra

Grab this branch if you want a solution file and a local clone of DataTables.Queryable so you can debug it on the fly.

https://github.com/VictorioBerra/DataTables.Queryable.ProjectTo.Issue.29/tree/dtqueryable-project-referenced

VictorioBerra avatar Nov 01 '18 19:11 VictorioBerra