Skip to content
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

Expression on interface implemented by entity throws InvalidOperationException #16759

Closed
nesherhh opened this issue Jul 26, 2019 · 3 comments
Closed
Assignees
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Milestone

Comments

@nesherhh
Copy link

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

	interface IHasId<out T>
	{
		T Id { get; }
	}

	class Entity : IHasId<int>
	{
		public virtual int Id { get; protected set; }
	}

	class Context : DbContext
	{
		public Context(DbContextOptions options) : base(options)
		{
		}

		public DbSet<Entity> Entities { get; set; }
	}

	class Specification<T>
		where T : IHasId<int>
	{
		public Expression<Func<T, bool>> Criteria { get; }

		public Specification(int id)
		{
			Criteria = t => t.Id == id;
		}
	}

	class Program
	{
		static void Main(string[] args)
		{
			var services = new ServiceCollection();
			services.AddDbContext<Context>(builder => builder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Database=test;Integrated Security=True"));
			var serviceProvider = services.BuildServiceProvider();

			var context = serviceProvider.GetRequiredService<Context>();
			context.Database.EnsureCreated();

			var specification = new Specification<Entity>(1);
			var entities = context.Set<Entity>().Where(specification.Criteria);
			var list = 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

@ajcvickers
Copy link
Member

ajcvickers commented Jul 26, 2019

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

@ajcvickers
Copy link
Member

Follow up with @smitpatel

@ajcvickers
Copy link
Member

Related to #13344 and #757. Moving to the backlog to consider when we tackle those issues.

@ajcvickers ajcvickers added this to the Backlog milestone Aug 5, 2019
smitpatel added a commit that referenced this issue Aug 27, 2019
smitpatel added a commit that referenced this issue Aug 27, 2019
smitpatel added a commit that referenced this issue Aug 27, 2019
@smitpatel smitpatel self-assigned this Aug 27, 2019
@smitpatel smitpatel modified the milestones: Backlog, 3.0.0 Aug 27, 2019
@smitpatel smitpatel added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Aug 27, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported type-enhancement
Projects
None yet
Development

No branches or pull requests

3 participants