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

ProjectTo doesn't work with EF Core 3 and GroupBy #3245

Closed
MihaMarkic opened this issue Oct 21, 2019 · 12 comments
Closed

ProjectTo doesn't work with EF Core 3 and GroupBy #3245

MihaMarkic opened this issue Oct 21, 2019 · 12 comments
Labels

Comments

@MihaMarkic
Copy link

MihaMarkic commented Oct 21, 2019

Source/destination types

// Entity
public class Party {
  public int Id { get; set; }
  public string Name { get; set; }
  public byte[] RowVersion { get; set; } // timestamp type in SQL Server
}

// Model is the same
public class PartyEditModel {
  public int Id { get; set; }
  public string Name { get; set; }
  public byte[] RowVersion { get; set; }
}

Mapping configuration

CreateMap<Party, PartyEditModel>();

Version: x.y.z

Automapper 9.0.0
EF Core 3.0.0

Expected behavior

db.Party.ProjectTo<PartyEditModel>(mapper.ConfigurationProvider).GroupBy(p => p.LastName).Select(g => new { g.Key, count = g.Count() }).ToArray();

Should group normally.

Actual behavior

An exception is thrown
System.InvalidCastException: 'Unable to cast object of type 'Microsoft.EntityFrameworkCore.Query.SqlExpressions.ColumnExpression' to type 'System.Linq.Expressions.ConstantExpression'.'

Steps to reproduce

Run this statement

`db.Party.ProjectTo<PartyEditModel>(mapper.ConfigurationProvider).GroupBy(p => p.LastName).Select(g => new { g.Key, count = g.Count() }).ToArray();`

Note that byte[] mapping is causing problems. When removed on model, the grouping works.

@lbargaoanu
Copy link
Member

ProjectTo should be the last call in the chain.

@MihaMarkic
Copy link
Author

@lbargaoanu Nope, why would it? EF Core can deal with projections as long as they are executed on serer side. And again, the problem is mapping of byte[] property (RowVersion in my sample) only, not with projections in general.

@lbargaoanu
Copy link
Member

lbargaoanu commented Oct 21, 2019

If it can deal with them, then just forget about AM and simply write that query using LINQ. Check the execution plan.

@MihaMarkic
Copy link
Author

So, instead of fixing what seems to be a bug, you are proposing to ditch AM instead?

@lbargaoanu
Copy link
Member

No, I'm explaining how to remove AM from the equation.

@MihaMarkic
Copy link
Author

Sorry, I don't follow. Why would I remove it? It's a great tool. Yet it somehow produces wrong mapping for byte array.

@jbogard
Copy link
Member

jbogard commented Oct 21, 2019 via email

@MihaMarkic
Copy link
Author

If it didn't work, I wouldn't be reporting it. I'll be triple checking it, but I'm pretty confident that it works if I do a projection without AM.
I'll try comparing expressions of possible as well.

@jbogard
Copy link
Member

jbogard commented Oct 21, 2019 via email

@MihaMarkic
Copy link
Author

Guys, sorry, was my fault afterall. After digging around, it was my custom ITypeConverter<byte[], byte[]> that is causing these problems. Which I added because of another problem regarding EF row versioning, but I'll create another ticket for it.

@MihaMarkic
Copy link
Author

I case you wonder why I went with custom type converter, here is the possible issue with EF Core and here is my stackoverflow question.

@lock
Copy link

lock bot commented Nov 22, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Nov 22, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants