Expression.NotEqual causing an exception when using queryable projections
See original GitHub issueFirst 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:
- Created 8 years ago
- Comments:16 (15 by maintainers)
Top GitHub Comments
This is working now without any changes. Well, ok then.
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.