efcore icon indicating copy to clipboard operation
efcore copied to clipboard

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

Open BenjiZombie opened this issue 8 years ago • 3 comments

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

BenjiZombie avatar Dec 09 '17 22:12 BenjiZombie

@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.

ajcvickers avatar Dec 11 '17 23:12 ajcvickers

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

BenjiZombie avatar Dec 12 '17 13:12 BenjiZombie

See #18685

smitpatel avatar Nov 20 '19 23:11 smitpatel