An exception is thrown with message when trying to order IQueryable
"EF.Property called with wrong property name."
C#
class CustomClass
{
Guid Id { get; set; }
string Name {get; set; }
}
public List<CustomClass> Get(DBContext context, string orderField)
{
var entities = context.Entity.AsQueryable();
entities = ApplyOrderField(entities, orderField);
var items = from entity in entities
select new CustomClass()
{
Id = entity.Id,
Name = entity.Name
};
return items.ToList(); // ==> excpetion thrown here
}
public IOrderedQueryable<T> ApplyOrderField<T>(IQueryable<T> items, string fieldName)
{
var orderedItems = items.OrderBy(e => EF.Property<object>(e, fieldName));
return orderedItems;
}
System.InvalidOperationException
HResult=0x80131509
Message=EF.Property called with wrong property name.
Source=Microsoft.EntityFrameworkCore.Relational
StackTrace:
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.Translate(Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateExpression(Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateLambdaExpression(ShapedQueryExpression shapedQueryExpression, LambdaExpression lambdaExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateOrderBy(ShapedQueryExpression source, LambdaExpression keySelector, Boolean ascending)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutorTResult
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQueryTResult
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCoreTResult
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass12_01.<ExecuteAsync>b__0()
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func1 compiler)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync[TResult](Expression query, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable1.GetAsyncEnumerator(CancellationToken cancellationToken)
at System.Runtime.CompilerServices.ConfiguredCancelableAsyncEnumerable1.GetAsyncEnumerator()
at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.<ToListAsync>d__641.MoveNext()
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
at Correspondence.Domain.BLL.Entity.L03.
This exception was originally thrown at this call stack:
[External Code]
Correspondence.Domain.BLL.Entity.L03.GetAsync(Correspondence.DAL.DB.CorrespondenceDBContext, Correspondence.Domain.Models.Entity.Get.RequestObject, System.Guid, System.Guid) in L03.cs
-->
EF Core version: 3.1.8
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET Core 3.0
Operating system: Win 10 x64
IDE: Visual Studio 2019 16.7.2
Note: the actual exception details copied here happened at a large project which uses async and other logic.
works in current bits.
Full repro:
[ConditionalFact]
public void Test22548()
{
using (var ctx = new MyContext())
{
ctx.Database.EnsureDeleted();
ctx.Database.EnsureCreated();
Get(ctx, "Name");
}
}
class CustomClass
{
public Guid Id { get; set; }
public string Name { get; set; }
}
class MyContext : DbContext
{
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<CustomClass>();
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=Repro22548;Trusted_Connection=True;MultipleActiveResultSets=true");
}
}
List<CustomClass> Get(MyContext context, string orderField)
{
var entities = context.Set<CustomClass>().AsQueryable();
entities = ApplyOrderField(entities, orderField);
var items = from entity in entities
select new CustomClass()
{
Id = entity.Id,
Name = entity.Name
};
return items.ToList();
}
generated sql:
SELECT [c].[Id], [c].[Name]
FROM [CustomClass] AS [c]
ORDER BY [c].[Name]
@AhmedMSedeek please try the code on our nightly builds, or provide a full runnable project that reproduces the issue on your end.
Attached a full working project that causes the exception
While I was creating the project I noticed that I made a mistake in the sample code I gave earlier, so sorry for that!
CREATE DATABASE [TestDB]
GO
USE [TestDB]
GO
CREATE TABLE [Entity]
(
[Id] UNIQUEIDENTIFIER NOT NULL CONSTRAINT [DF_Entity_Id] DEFAULT NEWID(),
[Name] NVARCHAR (250) NOT NULL,
CONSTRAINT [PK_Entity] PRIMARY KEY ([Id])
)
GO
INSERT INTO [Entity] ([Id], [Name]) VALUES
(NEWID(), N'Entity1'),
(NEWID(), N'Entity2')
@maumar
@AhmedMSedeek than you for the repro. I have verified that it fails in 3.1 and works correctly in current bits. Closing
Most helpful comment
works in current bits.
Full repro:
generated sql:
@AhmedMSedeek please try the code on our nightly builds, or provide a full runnable project that reproduces the issue on your end.