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

AutoMapper ProjectTo() blows up. #29

Open
VictorioBerra opened this issue Oct 30, 2018 · 4 comments
Open

AutoMapper ProjectTo() blows up. #29

VictorioBerra opened this issue Oct 30, 2018 · 4 comments

Comments

@VictorioBerra
Copy link
Contributor

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?

@AlexanderKrutov
Copy link
Owner

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.

@VictorioBerra
Copy link
Contributor Author

VictorioBerra commented Nov 1, 2018 via email

@VictorioBerra
Copy link
Contributor Author

VictorioBerra commented Nov 1, 2018

@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
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants