Repository navigation
Closed as not planned
Description
Steps to reproduce
public class Model
{
public Guid Id { get; set; }
public ChildModel Child { get; set; }
}
public class ChildModel
{
public Guid Id { get; set; }
public string Name { get; set; }
}
public class DTO {
public Guid Id { get; set; }
public Guid ChildId { get; set; }
}
var query = Db.Models.Select(x => new DTO {
Id = x.Id,
ChildId = x.Child.Id
});
var results = query.ToList(); // Fails with below exception
The issue
When projecting into a specific class, a expression type mismatch occurs.
System.ArgumentException : Argument types do not match
Stack Trace:
at System.Linq.Expressions.Expression.Bind(MemberInfo member, Expression expression)
at System.Linq.Expressions.MemberAssignment.Update(Expression expression)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberAssignment(MemberAssignment node)
at System.Linq.Expressions.ExpressionVisitor.VisitMemberBinding(MemberBinding node)
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.MemberInitExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression 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)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass19_0`1.<CompileQuery>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 Remotion.Linq.QueryableBase`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
Additional Information
Projecting to an anonymous type works as expected.
ChildId is of the expected type, Guid.
var query = Db.Models.Select(x => new {
Id = x.Id,
ChildId = x.Child.Id
});
Assert.Equal(typeof(Guid), query.ElementType.GetProperty("ChildId").PropertyType);
Projecting to a DTO where ChildId is a Nullable Guid also works.
public class DTO_With_Nullable {
public Guid Id { get; set; }
public Guid? ChildId { get; set; }
}
var query = Db.Models.Select(x => new DTO_With_Nullable {
Id = x.Id,
ChildId = x.Child.Id
});
Further technical details
EF Core version: 1.0.0
Operating system: Windows 10 x64, Version 1511
Visual Studio version: VS Code
Metadata
Metadata
Assignees
Labels
No labels
Activity
rowanmiller commentedon Jun 28, 2016
This is because the relationship is optional, so if the
Modeldoes not have an associatedChildModel(i.e. Model.Child would be null) then the value to be stored inDTO.ChildIdwould benull. For that reason, the value produced byx.Child.IdisNullable<Guid>.Interestingly, if this were a LINQ to Objects query, then you'd get a NullReferenceException if
Modeldidn't have a relatedChild, but EF is a little more forgiving.You could make the relationship required, if
Modelwill always have aChild:Or, you can defend against nulls in the query.
meekys commentedon Jun 29, 2016
Thanks. Figures I was doing something silly like that.
It's odd that this scenario works for an anonymous type, where the compiler generates a Guid member , but it doesn't work for a defined class with the same member type, or am I missing something here?
rowanmiller commentedon Jun 30, 2016
Closing as a duplicate of #5522. We want to change this so that we only throw if an actual null value comes back from the database.