Efcore: InvalidOperationException: No mapping to a relational type can be found for the CLR type 'object'.

Created on 18 Apr 2019  路  5Comments  路  Source: dotnet/efcore

A valid LINQ query which works over a List throws a System.InvalidOperationException when querying over a DbSet.

The expression is copied from an expression generated by AutoMapper.

Exception message: InvalidOperationException: No mapping to a relational type can be found for the CLR type 'object'.
Stack trace:    at Microsoft.EntityFrameworkCore.Storage.RelationalTypeMappingSourceExtensions.GetMapping(IRelationalTypeMappingSource typeMappingSource, Type clrType)
   at Microsoft.EntityFrameworkCore.Storage.TypeMaterializationInfo..ctor(Type modelClrType, IProperty property, IRelationalTypeMappingSource typeMappingSource, Nullable`1 fromLeftOuterJoin, Int32 index, RelationalTypeMapping mapping)
   at Microsoft.EntityFrameworkCore.Query.Expressions.SelectExpression.<GetMappedProjectionTypes>b__73_0(Expression e)
   at System.Linq.Enumerable.SelectListIterator`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Expressions.SelectExpression.<GetMappedProjectionTypes>d__73.MoveNext()
   at System.Collections.Generic.LargeArrayBuilder`1.AddRange(IEnumerable`1 items)
   at System.Collections.Generic.EnumerableHelpers.ToArray[T](IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at Microsoft.EntityFrameworkCore.Query.Sql.DefaultQuerySqlGenerator.CreateValueBufferFactory(IRelationalValueBufferFactoryFactory relationalValueBufferFactoryFactory, DbDataReader dataReader)
   at Microsoft.EntityFrameworkCore.Internal.NonCapturingLazyInitializer.EnsureInitialized[TParam,TValue](TValue& target, TParam param, Func`2 valueFactory)
   at Microsoft.EntityFrameworkCore.Query.Internal.ShaperCommandContext.NotifyReaderCreated(DbDataReader dataReader)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.<BufferlessMoveNext>d__12.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.EntityFrameworkCore.SqlServer.Storage.Internal.SqlServerExecutionStrategy.<ExecuteAsync>d__7`2.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.<MoveNext>d__11.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.AsyncSelectEnumerable`2.AsyncSelectEnumerator.<MoveNext>d__3.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.<CorrelateSubqueryAsync>d__17`3.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.TaskLiftingExpressionVisitor.<_ExecuteAsync>d__8`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.AsyncSelectEnumerable`2.AsyncSelectEnumerator.<MoveNext>d__3.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryBuffer.<CorrelateSubqueryAsync>d__17`3.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.TaskLiftingExpressionVisitor.<_ExecuteAsync>d__8`1.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.AsyncSelectEnumerable`2.AsyncSelectEnumerator.<MoveNext>d__3.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.<MoveNext>d__5.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Linq.AsyncEnumerable.<Aggregate_>d__6`3.MoveNext() in D:\a\1\s\Ix.NET\Source\System.Interactive.Async\Aggregate.cs:line 120
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at AutoMapperError.Shared.Replicator.<Replicate>d__0.MoveNext() in C:\Users\Joas\source\repos\AutoMapperError\AutoMapperError.Shared\Replicator.cs:line 147

Steps to reproduce

https://github.com/JoasE/EntityFrameworkError

c# var manualQuery = context.Courses.Select( dtoCourse => new Course.View { Id = dtoCourse.Id, Labs = dtoCourse.Labs.Select( dtoLab => new Lab.View { Assignments = dtoLab.Assignments .Select( dtoAssignment => new GeneratedType { __CurrentWork = dtoAssignment.UserAssignments.FirstOrDefault(x => x.UserId == userId), Id = dtoAssignment.Id, Lab = dtoAssignment.Lab, LabId = dtoAssignment.LabId, Name = dtoAssignment.Name }) .Select( dtoLet => new Assignment.View { CurrentWork = (dtoLet.__CurrentWork == null) ? null : new UserAssignment.View { AssignmentId = dtoLet.__CurrentWork.AssignmentId, StartedAt = dtoLet.__CurrentWork.StartedAt, UserId = dtoLet.__CurrentWork.UserId }, Id = dtoLet.Id, Lab = dtoLet.Lab, LabId = dtoLet.LabId, Name = dtoLet.Name }) .ToList(), CourseId = dtoLab.CourseId, Id = dtoLab.Id, Name = dtoLab.Name }).ToList(), Name = dtoCourse.Name, Order = dtoCourse.Order }); var manualCourses = await manualQuery.ToListAsync();

Further technical details

EF Core version: 2.2.4
Database Provider: Microsoft.EntityFrameworkCore.SqlServer 2.2.4
Operating system: Windows 10
IDE: Visual Studio 2017 Enterprise 15.9.6

closed-fixed customer-reported type-bug

All 5 comments

Thanks for the detailed issue @JoasE. I don't have too much experience with EF Core, but I'll try looking into this.

Just to confirm @ajcvickers, if the issue is only in EFCore 2.2, I submit my PR to the release/2.2 branch?

@j-rewerts By default, we fix for the next version, which is 3.0. In some cases we will consider also patching a released version, but at this time I don't think this issue meets the bar.

With regard to fixing this in master now, I would hold off, since we are in the middle of overhauling the query pipeline for 3.0. Once this is merged, then it will make sense to make other query changes. We hope to merge in the next few weeks. /cc @smitpatel @maumar

Is it okay to take off an issue that isn't related to the query pipeline, or should I just hold off until it's all merged?

@j-rewerts Yes, this only applies to query pipeline issues. Generally, look for any open issue in a milestone. (Being in a milestone means it has been triaged as something we want to do.) Issues tagged with up-for-grabs are supposed to be good entry-level issues, but we're not very good at applying the tag consistently, but it can still be useful when searching for something to do. See this query: https://github.com/aspnet/EntityFrameworkCore/issues?q=is%3Aopen+is%3Aissue+label%3Aup-for-grabs

I've tested this and can confirm this has been fixed as of EntityFramework 3.0.0. My thanks to the team and any contributor who might have helped for fixing this issue.

Was this page helpful?
0 / 5 - 0 ratings