0

MapsterMapper/Mapster 7.3.0

EFCore 7.0.5

asp.net core 7.0.0

I have two generic type in my BuzService class, one is TModel and other one is TEntity. TModel is a DTO class, TEntity is an entity class.

I have a method in the service class

public virtual async Task<int> UpdateAsync(Expression<Func<TModel, bool>> whereLambda,
    Expression<Func<TModel, TModel>> modelFac)
{
    //using Z.EntityFramework.Plus.EFCore extension method, T is an Entity class type.
    //public static Task<int> UpdateAsync<T>(this IQueryable<T> query,
    //                                    Expression<Func<T,T>>  updateFactory,
    //                                   CancellationToken cancellationToken = default(CancellationToken))

    return await DbContext.Set<TEntity>().AsQueryable()
            .Where(whereLambda.Adapt<Expression<Func<TEntity, bool>>>())
            .UpdateAsync(modelFac.Adapt<Expression<Func<TEntity, TEntity>>>());
}

whereLambda.Adapt<Expression<Func<TEntity, bool>> will throw a Mapster.CompileException with a message Cannot convert immutable type, please consider using 'MapWith' method to create mapping.

Can Mapster map Expression<Func<TModel, bool>> to Expression<Func<TEntity, bool>> and Expression<Func<TModel, TModel>> to Expression<Func<TEntity, TEntity>>? How to do it?

The code below shows how AutoMapper do this using AutoMapper.Extensions.ExpressionMapping


var entityWhere = Mapper.MapExpression<Expression<Func<TEntity, bool>>>(whereLambda);
var entityFac = Mapper.MapExpression<Expression<Func<TEntity, TEntity>>>(modelFac);
return await Repository.UpdateAsync(entityWhere, entityFac);

coolboy
  • 33
  • 4

0 Answers0