-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Query: Exception when projecting navigation property value that could return null, but does not ("Argument Types do not match") #5522
Comments
the problem is in here E = new E
{
Id = a.E.Id
} Because a.E is optional navigation, a.E.Id can potentially be null. To compensate we convert the calls to int? and now we are trying to fit int? into int column. Workaround is to use anonymous type instead: var item =
(from a
in db.As
select new
{
Id = a.Id,
EId = a.EId,
E = new
{
Id = (int?)a.E.Id
}
}
).First(); |
Talking to @maumar and @ajcvickers, we don't feel like this is RTM, because of the workaround and it doesn't seem to be that common. Thoughts? |
Thank you for the quick response. This actually doesn't solve the problem at all and is a major issue. My objective is to return a new E class, not a new anonymous class similar to E. A detached instance is going to the client as a web service result. I also tried using a nullable syntax in the query e.g. but that didn't work either. To resolve this, I would have to then map the result to the strong type definition and I can't use Automapper for anonymous types. This results in a ton of extra code and manual maintenance. |
Try this solution:
Generates following query
And does not throw above exception. |
Nope – the moment I add in the additional fields, it fails with the same error. The problem seems to be a lot deeper than just the key field and it applies to any model that has a parent with an optional child. B. From: Smit Patel [mailto:[email protected]] Try this solution: var item = Generates following query SELECT [a].[Id], [a].[CId], [a].[DId], [a].[EId], [a.E].[Id], 0 And does not throw above exception. ToList() version runs without any exception. (First() fails for me because my database doesn't have any data.) — |
As @maumar described above
For further properties added inside |
@smitpatel - this solutions works thanks ... uglifies my code but its functional. |
Super news - thank you so much for getting that knocked out. |
When doing this, also make sure the scenario from #5883 is taken care of - dotting thru an optional nav to get a value |
…Argument Types do not match" Problem was that during navigation rewrite we sometimes change types of expressions, e.g. o.Customer.Id (originally int) would get converted to: (o != null) ? (int?)o.CustomerId : null (change type to int?) We try to compensate for this later, by casting back to the original type, but we missed some cases: MemberAssignment, ElementInit, NewArray. Fix is to add the compensation for those nodes.
…Argument Types do not match" Problem was that during navigation rewrite we sometimes change types of expressions, e.g. o.Customer.Id (originally int) would get converted to: (o != null) ? (int?)o.CustomerId : null (change type to int?) We try to compensate for this later, by casting back to the original type, but we missed some cases: MemberAssignment, ElementInit, NewArray. Fix is to add the compensation for those nodes.
…Argument Types do not match" Problem was that during navigation rewrite we sometimes change types of expressions, e.g. o.Customer.Id (originally int) would get converted to: (o != null) ? (int?)o.CustomerId : null (change type to int?) We try to compensate for this later, by casting back to the original type, but we missed some cases: MemberAssignment, ElementInit, NewArray. Fix is to add the compensation for those nodes. CR: Smit
fixed in 7d359ab |
I'm running into a similar problem and I've tried all the workarounds and fixes but none of them worked. Here is my code and bear in mind I'm getting the same error. https://gist.github.com/DmsChrisPena/a8bfa560f5c2bd2565a778a67018dcd1 |
@DmsChrisPena the issue being tracked here is fixed. If you are still seeing the failure on our nightly builds, then please open a new issue with details. |
When doing this, also make sure the scenario from #5883 is taken care of - dotting thru an optional nav to get a value
If you have a model (see attached project) with class A with a nullable id field for child class E - int? EId, querying A->E fails with "argument types do not match" error.
In attached project query_ef_test_ac fails with the following information:
Test Name: BugDemoEF.EFTest.query_ef_test_ae
Test FullName: BugDemoEF.EFTest.query_ef_test_ae
Test Source: C:\Code\General\BugDemo\src\BugDemoEF\EFTests.cs : line 205
Test Outcome: Failed
Test Duration: 0:00:01.976
Result StackTrace:
at System.Linq.Expressions.Expression.Bind(MemberInfo member, Expression expression)
at System.Linq.Expressions.ExpressionVisitor.Visit[T](ReadOnlyCollection
1 nodes, Func
2 elementVisitor)at System.Linq.Expressions.ExpressionVisitor.VisitMemberInit(MemberInitExpression node)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberAssignment(MemberAssignment node)
at System.Linq.Expressions.ExpressionVisitor.Visit[T](ReadOnlyCollection
1 nodes, Func
2 elementVisitor)at System.Linq.Expressions.ExpressionVisitor.VisitMemberInit(MemberInitExpression node)
at Remotion.Linq.Clauses.SelectClause.TransformExpressions(Func
2 transformation) at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel(QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.NavigationRewritingExpressionVisitor.Rewrite(QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.OptimizeQueryModel(QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass19_0
1.b__0()at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at BugDemoEF.EFTest.query_ef_test_ae() in C:\Code\General\BugDemo\src\BugDemoEF\EFTests.cs:line 208
Result Message: Argument types do not match
BugDemo.zip
The text was updated successfully, but these errors were encountered: