我正在使用AddAutoMapper
NuGet软件包中提供的AutoMapper.Extensions.Microsoft.DependencyInjection
方法将AutoMapper设置为ASP.NET Core 2.2 Web应用程序。
在将各个实体映射到模型以及返回模型时,效果很好-我可以毫无问题地做到这一点:
mapper.Map<Model>(context.Set<Entity>().First());
但是我不知道如何使ProjectTo
工作。我正在注入IMapper
,并且已经尝试了这两种方法:
mapper.ProjectTo<Model>(context.Set<Entity>());
和
context.Set<Entity>().ProjectTo<Model>(mapper.ConfigurationProvider);
无论哪种方式,我都会遇到相同的错误
System.ArgumentException: Static method requires null instance, non-static method requires non-null instance.
at System.Linq.Expressions.Expression.ValidateStaticOrInstanceMethod(Expression instance, MethodInfo method)
at System.Linq.Expressions.Expression.ValidateMethodAndGetParameters(Expression instance, MethodInfo method)
at System.Linq.Expressions.Expression.Call(MethodInfo method, Expression arg0)
at AutoMapper.QueryableExtensions.Impl.MemberGetterExpressionResultConverter.ExpressionResolutionResult(ExpressionResolutionResult expressionResolutionResult, MemberInfo getter) in C:\projects\automapper\src\AutoMapper\QueryableExtensions\Impl\MemberGetterExpressionResultConverter.cs:line 24
at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable`1 source, TAccumulate seed, Func`3 func)
at AutoMapper.QueryableExtensions.ExpressionBuilder.ResolveExpression(PropertyMap propertyMap, Type currentType, Expression instanceParameter, LetPropertyMaps letPropertyMaps) in C:\projects\automapper\src\AutoMapper\QueryableExtensions\ExpressionBuilder.cs:line 308
at AutoMapper.QueryableExtensions.ExpressionBuilder.<>c__DisplayClass17_0.<CreateMemberBindings>g__CreateMemberBinding|0(PropertyMap propertyMap) in C:\projects\automapper\src\AutoMapper\QueryableExtensions\ExpressionBuilder.cs:line 272
at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMemberBindings(ExpressionRequest request, TypeMap typeMap, Expression instanceParameter, IDictionary`2 typePairCount, LetPropertyMaps letPropertyMaps) in C:\projects\automapper\src\AutoMapper\QueryableExtensions\ExpressionBuilder.cs:line 266
at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpressionCore(ExpressionRequest request, Expression instanceParameter, IDictionary`2 typePairCount, TypeMap typeMap, LetPropertyMaps letPropertyMaps) in C:\projects\automapper\src\AutoMapper\QueryableExtensions\ExpressionBuilder.cs:line 204
at AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression(ExpressionRequest request, IDictionary`2 typePairCount, LetPropertyMaps letPropertyMaps) in C:\projects\automapper\src\AutoMapper\QueryableExtensions\ExpressionBuilder.cs:line 152
at System.Lazy`1.ViaFactory(LazyThreadSafetyMode mode)
at System.Lazy`1.ExecutionAndPublication(LazyHelper executionAndPublication, Boolean useDefaultConstructor)
at System.Lazy`1.CreateValue()
at AutoMapper.QueryableExtensions.ExpressionBuilder.GetMapExpression(Type sourceType, Type destinationType, Object parameters, MemberInfo[] membersToExpand) in C:\projects\automapper\src\AutoMapper\QueryableExtensions\ExpressionBuilder.cs:line 98
at AutoMapper.QueryableExtensions.ProjectionExpression.To[TResult](Object parameters, Expression`1[] membersToExpand) in C:\projects\automapper\src\AutoMapper\QueryableExtensions\ProjectionExpression.cs:line 60
at AutoMapper.QueryableExtensions.Extensions.ProjectTo[TDestination](IQueryable source, IConfigurationProvider configuration, Expression`1[] membersToExpand) in C:\projects\automapper\src\AutoMapper\QueryableExtensions\Extensions.cs:line 76
我正在尝试按照此处的文档所述使用它:
但是看起来我在静态和实例设置上做错了。我想使用我通过DI传递的IMapper
实例,因为它在单个地图上可以正常工作。
该如何解决该错误?
答案 0 :(得分:1)
我找出了此错误的原因。感谢Lucian Bargaoanu提醒您有限的supported mapping options。我使用flattening并在源类型上使用以“ Get”为前缀的方法,但是我认为ProjectTo
不适用。 (我认为这属于列表的最后一项-域对象上的任何计算属性。)这就是LINQ给出错误“非静态方法需要非null实例”的原因。 / p>
答案 1 :(得分:0)
不确定这是否对任何人有帮助,但不要忘记检查您的反向映射是否也符合 AutoMapper 支持的选项。
为了解决这个问题,我花了一个小时的时间反复试验。最后,我注意到/记得我在所有映射的末尾都有 ReverseMap()
,这意味着当映射被反转时,违反了大量的源到目标映射规则。