Skip to content

How to map SetPropertyCalls<T>? #185

@gordon-matt

Description

@gordon-matt

I am trying to do something like this:

int affectedRows = realRepository.Update(
    p => p.ProductModelId == 5,
    s => s.SetProperty(p => p.Name, newName));

Where that method is defined on a generic repository as follows:

public virtual int Update(
    Expression<Func<TModel, bool>> predicate,
	Expression<Func<SetPropertyCalls<TModel>, SetPropertyCalls<TModel>>> setPropertyCalls,
	ContextOptions options = null)
{
    var mappedPredicate = entityModelMapper.MapPredicate(predicate);
    var mappedSetPropertyCalls = entityModelMapper.MapSetPropertyCalls(setPropertyCalls);
    using var context = GetContext(options);
    return context.Set<TEntity>().Where(mappedPredicate).ExecuteUpdate(mappedSetPropertyCalls);
}

And entityModelMapper.MapSetPropertyCalls looks like this:

 public Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>> MapSetPropertyCalls(
     Expression<Func<SetPropertyCalls<TModel>, SetPropertyCalls<TModel>>> setPropertyCalls) =>
     mapper.MapExpression<Expression<Func<SetPropertyCalls<TEntity>, SetPropertyCalls<TEntity>>>>(setPropertyCalls);

This is obviously not working. The unit test throws this error:

Message: 
System.InvalidOperationException : No generic method 'SetProperty' on type 'Microsoft.EntityFrameworkCore.Query.SetPropertyCalls`1[Extenso.TestLib.Data.Entities.ProductModel]' is compatible with the supplied type arguments and arguments. No type arguments should be provided if the method is non-generic. 

  Stack Trace: 
Expression.FindMethod(Type type, String methodName, Type[] typeArgs, Expression[] args, BindingFlags flags)
Expression.Call(Expression instance, String methodName, Type[] typeArguments, Expression[] arguments)
<>c__DisplayClass39_0.<VisitMethodCall>g__GetInstanceExpression|2(Expression instance)
XpressionMapperVisitor.VisitMethodCall(MethodCallExpression node)
MapperExtensions.<MapExpression>g__MapBody|8_1[TDestDelegate](Dictionary`2 typeMappings, XpressionMapperVisitor visitor, <>c__DisplayClass8_0`1&)
MapperExtensions.<MapExpression>g__CreateVisitor|8_0[TDestDelegate](Dictionary`2 typeMappings, <>c__DisplayClass8_0`1&)
MapperExtensions.MapExpression[TDestDelegate](IConfigurationProvider configurationProvider, LambdaExpression expression, Type typeSourceFunc, Type typeDestFunc, Func`3 getVisitor, Func`2 shouldConvertMappedBodyToDestType)
MapperExtensions.MapExpression[TDestDelegate](IMapper mapper, LambdaExpression expression, Func`3 getVisitor, Func`2 shouldConvertMappedBodyToDestType)
MapperExtensions.MapExpression[TDestDelegate](IMapper mapper, LambdaExpression expression)
AutoMapperEntityModelMapper`2.MapSetPropertyCalls(Expression`1 setPropertyCalls) line 42
MappedEntityFrameworkRepository`2.Update(Expression`1 predicate, Expression`1 setPropertyCalls, ContextOptions options) line 552
AutoMapperEntityFrameworkRepositoryTests.Update_WithPredicateAndSetPropertyCalls() line 806
RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions