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

Query: GetHashCode(Expression obj) of ExpressionEqualityComparer does not support ExpressionType.Assign #10529

Closed
BenjiZombie opened this issue Dec 9, 2017 · 3 comments · Fixed by #19271
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. good first issue This issue should be relatively straightforward to fix. punted-for-2.1 type-bug
Milestone

Comments

@BenjiZombie
Copy link

BenjiZombie commented Dec 9, 2017

Hi there,

I have a homemade mapping/projection library that I use to map/project between DB models and DTOs. In the expression tree that this library builds, there are a few assignments (i.e. Expression.Assign) being made in the logic to support mapping/projection of abstract classes to derived types (i.e. I have the abstract Field, and the concrete BooleanField entities and the abstract FieldDto, and the concret BooleanFieldDto DTOs).

Here is the relevant part in the resulting expression tree:

.If (
	$field .Is Application.Domain.Models.BooleanField
) {
	.Block(Application.Domain.Models.BooleanField $booleanfield) {
		$booleanfield = (Application.Domain.Models.BooleanField)$field;
		$x = (Web.Models.Schemas.FieldDto).New Web.Models.Schemas.BooleanFieldDto(){
			Properties = .New Web.Models.Schemas.BooleanFieldPropertiesDto(){
				DefaultValue = ($booleanfield.Properties).DefaultValue,
				Label = ($booleanfield.Properties).Label,
				Hint = ($booleanfield.Properties).Hint,
				Placeholder = ($booleanfield.Properties).Placeholder,
				IsRequired = ($booleanfield.Properties).IsRequired
			},
			Name = $booleanfield.Name,
			FieldTypeName = $booleanfield.FieldTypeName
		}
	}
} .Else {
	.Default(System.Void)
};

Mapping an existing entity model to its DTO counterpart works as expected, however doing a projection fails with the following exception:

An unhandled exception occurred while processing the request.

NotImplementedException: The method or operation is not implemented.

Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode(Expression obj)
Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode(Expression obj)
Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode<T>(IList<T> expressions)
Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode(Expression obj)
Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode<T>(IList<T> expressions)
Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode(Expression obj)
Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode(Expression obj)
Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode(Expression obj)
Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode(Expression obj)
Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode<T>(IList<T> expressions)
Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode(Expression obj)
Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode<T>(IList<T> expressions)
Microsoft.EntityFrameworkCore.Query.Internal.ExpressionEqualityComparer.GetHashCode(Expression obj)
Microsoft.EntityFrameworkCore.Query.CompiledQueryCacheKeyGenerator+CompiledQueryCacheKey.GetHashCode()
Microsoft.EntityFrameworkCore.Query.RelationalCompiledQueryCacheKeyGenerator+RelationalCompiledQueryCacheKey.GetHashCode()
System.Collections.Generic.ObjectEqualityComparer.GetHashCode(T obj)
System.Collections.Concurrent.ConcurrentDictionary.TryGetValue(TKey key, out TValue value)
Microsoft.Extensions.Caching.Memory.MemoryCache.TryGetValue(object key, out object result)
Microsoft.Extensions.Caching.Memory.CacheExtensions.TryGetValue<TItem>(IMemoryCache cache, object key, out TItem value)
Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore<TFunc>(object cacheKey, Func<Func<QueryContext, TFunc>> compiler)
Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddAsyncQuery<TResult>(object cacheKey, Func<Func<QueryContext, IAsyncEnumerable<TResult>>> compiler)
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileAsyncQuery<TResult>(Expression query)
Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteAsync<TResult>(Expression query, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.ExecuteAsync<TResult>(Expression expression, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync<TSource, TResult>(MethodInfo operatorMethodInfo, IQueryable<TSource> source, CancellationToken cancellationToken)
Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleOrDefaultAsync<TSource>(IQueryable<TSource> source, CancellationToken cancellationToken)
HedgeSoft.Web.Controllers.SchemasController+<Get>d__5.MoveNext() in SchemasController.cs
var schema = await _mapper.Project<SchemaDetailedDto>(

I noticed in the code of ExpressionEqualityComparer.GetHashCode(Expression obj), there is no case for ExpressionType.Assign, hence the NotImplementedException thrown above.

Is this something that is not supported, or will be supported by EF?

As a workaround, I am fetching the entity normally, then doing a map to a DTO. However I would prefer support for projection for performance reasons.

Thanks in advance!

Further technical details

EF Core version: 2.0.0
Database Provider: NpgSql
Operating system: Windows 10 Pro
IDE: Visual Studio 2017 15.5.1

@ajcvickers ajcvickers added this to the Backlog milestone Dec 11, 2017
@ajcvickers ajcvickers added help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. type-bug labels Dec 11, 2017
@ajcvickers ajcvickers modified the milestones: Backlog, 2.1.0 Dec 11, 2017
@ajcvickers
Copy link
Contributor

@BenjiZombie We will try to get a fix for this into 2.1, although it might not make it. However, fixing this exception might still result in an expression tree that EF cannot translate. We prioritize expression trees that come from lambda expressions directly over arbitrary trees that can be build manually or by other tools.

You might want to consider submitting a PR to get this fixed sooner.

@BenjiZombie
Copy link
Author

Thanks for the quick reply. I'll investigate doing a PR if time allows.

@ajcvickers ajcvickers modified the milestones: 2.1.0-preview1, 2.1.0 Jan 17, 2018
@ajcvickers ajcvickers modified the milestones: 2.1.0, Backlog Jan 26, 2018
@ajcvickers ajcvickers changed the title GetHashCode(Expression obj) of ExpressionEqualityComparer does not support ExpressionType.Assign Query: GetHashCode(Expression obj) of ExpressionEqualityComparer does not support ExpressionType.Assign May 16, 2018
@smitpatel smitpatel removed their assignment Sep 1, 2018
@divega divega added good first issue This issue should be relatively straightforward to fix. help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. and removed help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. good first issue This issue should be relatively straightforward to fix. labels May 31, 2019
@ajcvickers ajcvickers added good first issue This issue should be relatively straightforward to fix. and removed help wanted This issue involves technologies where we are not experts. Expert help would be appreciated. labels Aug 5, 2019
@ajcvickers ajcvickers removed the good first issue This issue should be relatively straightforward to fix. label Aug 30, 2019
@smitpatel smitpatel added the good first issue This issue should be relatively straightforward to fix. label Sep 4, 2019
@smitpatel
Copy link
Contributor

See #18685

smitpatel added a commit that referenced this issue Dec 11, 2019
smitpatel added a commit that referenced this issue Dec 11, 2019
smitpatel added a commit that referenced this issue Dec 11, 2019
@smitpatel smitpatel self-assigned this Dec 12, 2019
@smitpatel smitpatel modified the milestones: Backlog, 5.0.0 Dec 12, 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 Dec 12, 2019
smitpatel added a commit that referenced this issue Dec 12, 2019
@ajcvickers ajcvickers modified the milestones: 5.0.0, 5.0.0-preview1 Mar 13, 2020
@ajcvickers ajcvickers modified the milestones: 5.0.0-preview1, 5.0.0 Nov 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. good first issue This issue should be relatively straightforward to fix. punted-for-2.1 type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants