简体   繁体   English

自动映射器表达式映射

[英]Automapper expression mapping

I am trying to perform the following Automapper mapping for an OrderBy: 我正在尝试为OrderBy执行以下Automapper映射:

Expression<Func<ServerObject, object>> serverQueryable = x => x.TestEnumKVP.Value;
Mapper.Map<Expression<Func<ServerObject, object>>, Expression<Func<DatabaseObject, object>>(serverQueryable)

I want to map the ServerObject expression to a DatabaseObject expression 我想将ServerObject表达式映射到DatabaseObject表达式

ServerObject defined as: ServerObject定义为:

public class ServerObject
{
    public KeyValuePairEx TestEnumKVP { get; set; }
}

KeyValuePairEx is a wrapper for the Enumeration which stores the Int16 value and the string value: KeyValuePairEx是Enumeration的包装,该包装存储Int16值和字符串值:

public enum TestEnum : Int16 { Test1, Test2, Test3 }

public class KeyValuePairEx
{
    internal KeyValuePairEx(TestEnum key, string value) { }

    public TestEnum Key { get; set; }
    public string Value { get; set; }
}

DatabaseObject defined as: DatabaseObject定义为:

public class DatabaseObject
{
    public string TestEnumId { get; set; }
}

The Mapping I have is: 我的映射是:

AutoMapper.Mapper.Initialize(config =>
{
    config.CreateMap<DatabaseObject, ServerObject>().ForMember(dest => dest.TestEnumKVP.Value, opt => opt.MapFrom(src => src.TestEnumId));
});

The mapping fails with: 映射失败,并显示:

'Expression 'dest => dest.TestEnumKVP.Value' must resolve to top-level member and not any child object's properties. “表达式'dest => dest.TestEnumKVP.Value”必须解析为顶级成员,而不是任何子对象的属性。 Use a custom resolver on the child type or the AfterMap option instead.' 在子类型上使用自定义解析器,或在AfterMap选项上使用。”

I need ServerObject.TestEnumKVP.Value to Map to DatabaseObject.TestEnumId. 我需要ServerObject.TestEnumKVP.Value映射到DatabaseObject.TestEnumId。 I am aware that Expression mappings are reversed - hence why the Map is from DatabaseObject to ServerObject. 我知道表达式映射是反向的-因此为什么映射是从DatabaseObject到ServerObject。 I have spent many hours on this and am at a loss as to how to get the mapping to work! 我在此上花费了许多时间,但对于如何使映射正常工作却一无所知!

NB. NB。 I am using AutoMapper 6.1.1 我正在使用AutoMapper 6.1.1

Any help would be appreciated! 任何帮助,将不胜感激!

Thank you Lucian, I followed the github link and the solution offered by Blaise has worked. 谢谢Lucian,我遵循了github链接,Blaise提供的解决方案已经奏效。 See below: 见下文:

CreateMap<DatabaseObject, ServerObject>().ForMember(dest => dest.TestEnumKVP, opt => opt.MapFrom(src => src));
CreateMap<DatabaseObject, KeyValuePairEx>().ForMember(dest => dest.Value, opt => opt.MapFrom(src => src.TestEnumId));

I was starting to look for at workarounds so delighted it was possible and that the solution was so clean and concise. 我开始寻找可行的解决方法,使它感到很高兴,并且解决方案简洁明了。

Thanks again! 再次感谢!

The error and the solution are right there in the message. 错误和解决方案就在消息中。 Forget about all the expression stuff. 忘记所有的表达方式。 The ForMember is broken. ForMember已损坏。 Try ForPath instead. 请尝试使用ForPath。 Expression mapping now supports ForPath. 表达式映射现在支持ForPath。 See https://github.com/AutoMapper/AutoMapper/issues/2293 . 参见https://github.com/AutoMapper/AutoMapper/issues/2293

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM