简体   繁体   中英

AutoMapper using built-in mappers

Upgrading from AutoMapper 4.2.1 to 5.0.0 I can no longer do this without providing configuration:

var entity = Mapper.Map<Dictionary<string, object>, MyEntity>(feature.Attributes);

Previously AutoMapper would automatically work out that the Dictionary keys map directly to property names without any setup.

I've tried initializing using:

Mapper.Initialize(cfg =>
{
    cfg.CreateMap<Dictionary<string, object>, MyEntity>();
    cfg.CreateMap<Dictionary<string, object>, MyOtherEntity>();
});

But this results in null properties in my entities as AutoMapper is no longer automatically wiring up it's previous built-in Dictionary mapper this way.

I assume I need to use something like ProjectUsing() but I've not managed to locate the built-in AutoMapper mappers yet.

I'm sure this is a really simple code problem to resolve. Any quick pointers? Or even link to relevant documentation that I may have somehow missed? Thanks in advance!

You can use dynamic map:

var dic = new Dictionary<string, string>();
dic.Add("foo", "bar");

var test = Mapper.DynamicMap<Dictionary<string, string>>(dic);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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