Skip to content

Mapster.EFCore ProjectToType<T>().SingleOrDefault throwing exception #566

@MortenRickiRasmussen

Description

@MortenRickiRasmussen

Hi!

I have this piece of code

public async Task<T> SingleOrDefault(int id) 
{
  var query = DbContext.SomeEntity.Where(x => x.Id == id);

   return await _mapper.From(query).ProjectToType<T>().SingleOrDefaultAsync();
}

it throws the following Exception

Data: System.MissingMethodException: Constructor on type 'Mapster.EFCore.MapsterAsyncEnumerable`1[[SomeSystem.SomeDto, SomeSystem.Api, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' not found.
   at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture)
   at Mapster.EFCore.MapsterQueryableProvider.ExecuteAsync[TResult](Expression expression, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, Expression expression, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ExecuteAsync[TSource,TResult](MethodInfo operatorMethodInfo, IQueryable`1 source, CancellationToken cancellationToken)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.SingleOrDefaultAsync[TSource](IQueryable`1 source, CancellationToken cancellationToken) .........

If i exchange the code to the following, it works fine

public async Task<T> SingleOrDefault(int id) 
{
   var query = DbContext.SomeEntity.Where(x => x.Id == id);

   var list = await _mapper.From(query).ProjectToType<T>().ToListAsync();

   return list.FirstOrDefault();
}

Additional info:

AspNet 7.0
Mapster 7.3.0
Mapster.EFCore 5.1.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions