Skip to content

Regression in v10 for record types when using ProjectToType #881

@DocSvartz

Description

@DocSvartz

Mapster ProjectToType fails with Mapping is invalid for projection

Description

When using ProjectToType<TResult>() over an IQueryable (EF Core), Mapster throws a CompileException indicating that the mapping is not valid for projection.

This occurs even when the mapping appears straightforward and no obvious custom logic is involved.


Exception

Mapster.CompileException: Error while compiling
source=WebApi.Example.Domain.Entities.Catalogos.Lugar
destination=WebApi.Example.Controllers.LugarDto
type=Projection
 ---> System.InvalidOperationException: Mapping is invalid for projection
   at Mapster.Adapters.BaseAdapter.CreateBlockExpressionBody(Expression source, Expression destination, CompileArgument arg)
   at Mapster.Adapters.BaseAdapter.CreateExpressionBody(Expression source, Expression destination, CompileArgument arg)
   at Mapster.Adapters.BaseAdapter.CreateAdaptFunc(CompileArgument arg)
   at Mapster.TypeAdapterConfig.CreateMapExpression(CompileArgument arg)
   --- End of inner exception stack trace ---
   at Mapster.TypeAdapterConfig.CreateMapExpression(CompileArgument arg)
   at Mapster.TypeAdapterConfig.CreateMapExpression(TypeTuple tuple, MapType mapType)
   at Mapster.TypeAdapterConfig.CreateProjectionCallExpression(TypeTuple tuple)
   at Mapster.TypeAdapterConfig.<>c__DisplayClass60_0`1.<AddToHash>b__0(TypeTuple types)
   at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at Mapster.TypeAdapterConfig.AddToHash[T](ConcurrentDictionary`2 hash, TypeTuple key, Func`2 func)
   at Mapster.TypeAdapterConfig.GetProjectionCallExpression(Type sourceType, Type destinationType)
   at Mapster.Extensions.ProjectToType[TDestination](IQueryable source, TypeAdapterConfig config)
   at Infrastructure.Persistence.Repositories.ApplicationRepositoryBase`1.ApplySpecification[TResult](ISpecification`2 specification)

Code Context

    public class ApplicationRepositoryBase<T>(DbContext dbContext, ICacheService cache) : RepositoryBase<T>(dbContext), IAppRepositoryBase<T> where T : class, IBaseEntity
    {
        private readonly DbContext _dbContext = dbContext;
protected override IQueryable<TResult> ApplySpecification<TResult>(ISpecification<T, TResult> specification)
{
    if (specification.Selector is not null)
    {
        return base.ApplySpecification(specification);
    }

    var query = ApplySpecification(specification, false);
    return query.ProjectToType<TResult>();
}
......
}

Expected Behavior

ProjectToType() should generate a valid expression tree that can be translated by EF Core into SQL, as long as the mapping only involves simple property projections.

Actual Behavior

Mapster fails during expression compilation with:

Mapping is invalid for projection

There is no clear indication of which member or mapping rule is causing the issue.

Additional Context
Using Ardalis.Specification with EF Core
Projection is applied only when specification.Selector is null
The same mapping works correctly when executed in-memory using .Adapt()
Questions
Is there a way to get more detailed diagnostics about which part of the mapping is not translatable?
Are there known limitations or guidelines for using ProjectToType with EF Core?

using

Image

We also tested EFCoreProjectToType, but encountered the same projection error.

Originally posted by @frankyjquintero in #875

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions