question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Expression.NotEqual causing an exception when using queryable projections

See original GitHub issue

Here is the repro repo.

First of all I’m not sure if this problem is ef (or even ef7) specific, but here goes.

We have two simple models with a one-to-many relationship between them, Article and Category. We also have two DTOs related to them. So we configure the mappings:

Mapper.Initialize(config =>
    {
        config.CreateMap<Category, CategoryDto>();
        config.CreateMap<Article, ArticleDto>();
    });

A simple query like the following directly throws an exception:

_context.Articles.ProjectTo<ArticleDto>().ToList()
InvalidOperationException: The binary operator NotEqual is not defined for the types 'System.Int32' and 'System.Object'.

I followed the problem to its source, the MappedTypeExpressionBinder is generating a condition on Category to “handle null source property so it will not create an object with possible non-nullable properties which would result in an exception”. You can see the logic here.

This works for optional relationships but for a required one (like the one in my repro) this actually throws an exception because of an incorrect generated comparison logic between int (the table key) and object (null). Probably because ef is seeing that we’re checking for null over the navigation property, so it figures that the relationship is “nullable”.

One workaround is to set Mapper.Configuration.AllowNullDestinationValues to false but this is far from a solution because we might actually get into the situation that is described as a reason for the very same generated condition.

This might be a bug in ef7 itself, so I’m not sure but at least can’t we provide a solution for this in AutoMapper?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:16 (15 by maintainers)

github_iconTop GitHub Comments

1reaction
jbogardcommented, May 16, 2016

This is working now without any changes. Well, ok then.

0reactions
lock[bot]commented, May 7, 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AutoMapper strange IQueryable projection exception
This works when mapping to a SemesterDto directly, but fails when SemesterDto is nested inside another dto just like before but with different ......
Read more >
More Effective C#: 50 Specific Ways to Improve Your C
Every query expression has a mapping to a method call or calls. You should understand this mapping from two perspectives. From the perspective...
Read more >
Client-side casting with LINQ3 projection throws ...
Same exception occurs when using aggregation pipeline with AsQueryable().Where().Select(). Changing the cast to something like a.ArrayOfX.Where( ...
Read more >
Avoiding NotSupportedException with IQueryable
One solution is to move the entire projection out of the expression tree by switching from IQueryable to IEnumerable (using the AsEnumerable ...
Read more >
Automapper nullable reference types. RoomType, opt => ...
It fails when I'm using this property in projection e. ... AutoMapper will ignore null reference exceptions when mapping your source to your...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found