You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have an Expression that is used to filter entities by Id. The Expression is based on the interface that is implemented by the entity (see the code below). This throws InvalidOperationException.
Exception message: Operation is not valid due to the current state of the object.
Stack trace:
System.InvalidOperationException
HResult=0x80131509
Message=Operation is not valid due to the current state of the object.
Source=Microsoft.EntityFrameworkCore.Relational
StackTrace:
at Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateWhere(ShapedQueryExpression source, LambdaExpression predicate) in /_/src/EFCore.Relational/Query/Pipeline/RelationalQueryableMethodTranslatingExpressionVisitor.cs:line 867
at Microsoft.EntityFrameworkCore.Query.Pipeline.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) in /_/src/EFCore/Query/Pipeline/QueryableMethodTranslatingExpressionVisitor.cs:line 424
at Microsoft.EntityFrameworkCore.Relational.Query.Pipeline.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression) in /_/src/EFCore.Relational/Query/Pipeline/RelationalQueryableMethodTranslatingExpressionVisitor.cs:line 61
at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
at Microsoft.EntityFrameworkCore.Query.Pipeline.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query) in /_/src/EFCore/Query/Pipeline/QueryCompilationContext.cs:line 65
at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async) in /_/src/EFCore/Storage/Database.cs:line 72
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async) in /_/src/EFCore/Query/Internal/QueryCompiler.cs:line 108
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0() in /_/src/EFCore/Query/Internal/QueryCompiler.cs:line 97
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler) in /_/src/EFCore/Query/Internal/CompiledQueryCache.cs:line 84
at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler) in /_/src/EFCore/Query/Internal/CompiledQueryCache.cs:line 59
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query) in /_/src/EFCore/Query/Internal/QueryCompiler.cs:line 93
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression) in /_/src/EFCore/Query/Internal/EntityQueryProvider.cs:line 79
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator() in /_/src/EFCore/Query/Internal/EntityQueryable`.cs:line 94
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at EfBug.Program.Main(String[] args) in C:\tmp\EfBug\EfBug\Program.cs:line 52
Steps to reproduce
interfaceIHasId<outT>{TId{get;}}classEntity:IHasId<int>{publicvirtualintId{get;protectedset;}}classContext:DbContext{publicContext(DbContextOptionsoptions):base(options){}publicDbSet<Entity>Entities{get;set;}}classSpecification<T>whereT:IHasId<int>{publicExpression<Func<T,bool>>Criteria{get;}publicSpecification(intid){Criteria= t =>t.Id==id;}}classProgram{staticvoidMain(string[]args){varservices=newServiceCollection();services.AddDbContext<Context>(builder =>builder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Database=test;Integrated Security=True"));varserviceProvider=services.BuildServiceProvider();varcontext=serviceProvider.GetRequiredService<Context>();context.Database.EnsureCreated();varspecification=newSpecification<Entity>(1);varentities=context.Set<Entity>().Where(specification.Criteria);varlist=entities.ToList();}}
Further technical details
EF Core version: 3.0.0-preview7.19362.6
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10 Build 18362.239
IDE: Visual Studio 2019 16.3.0 Preview 1.0
The text was updated successfully, but these errors were encountered:
Note for triage: confirmed that this repros on current nightlies and that the issue is only hit when the Specification class is generic on the interface.
In other words, the query works if where T : IHasId<int> is changed to where T : Entity
I have an Expression that is used to filter entities by Id. The Expression is based on the interface that is implemented by the entity (see the code below). This throws InvalidOperationException.
Steps to reproduce
Further technical details
EF Core version: 3.0.0-preview7.19362.6
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Windows 10 Build 18362.239
IDE: Visual Studio 2019 16.3.0 Preview 1.0
The text was updated successfully, but these errors were encountered: