A few days ago found a performance problem with AutoMapper 3.3 (see: Encountering AutoMapper Performance issues: Mapping 200 data is nearly thousands of years slower than 100), so the AutoMapper upgrade to the latest 5.1.1 see if this performance problem also exists.
The previous map configuration code is taken for granted in the upgrade:
Mapper.createmap<aentity, adto>(); Mapper.createmap<bentity, bdto> ();
Switch
Mapper.initialize (cfg = cfg. Createmap<aentity, adto>= cfg. Createmap<bentity, bdto> ());
However, the following error occurred while the code was running when Mapper.map () was executed:
Missing type map configuration or unsupported mapping
The following error occurred while executing Projectto ():
System.InvalidOperationException:Missing map from Aentity to Adto. Create using Mapper.createmap<aentity, Adto>. In AutoMapper.QueryableExtensions.ExpressionBuilder.CreateMapExpression ...
At that time, baffled the solution, tossing for a while without fruit, and temporarily put aside.
Today, on the BO asked that someone has encountered the same problem-solve AutoMapper mapping class error Solver, so the interest to solve this problem is stimulated, and began to toss.
In the process of tossing and finding, if not mapper.initialize () This static configuration mode, instead of using the following instance configuration, this problem will not occur.
var New Mapperconfiguration (cfg = cfg. Createmap<a, b>()); var mapper = config. Createmapper (); mapper. Map<List<B>> (List);
Mapper.initialize () naturally became the biggest suspect, so from GitHub to check out the AutoMapper source code a look at the realization of mapper.initialize (), suddenly.
Public Static void Initialize (action<imapperconfigurationexpression> config) { new Mapperconfiguration (config); New Mapper (Configuration);}
Each call to Mapper.initialize () will create a new Mapper instance, that is, multiple calls to Mapper.initialize () are only valid for the last time.
The following configuration code only takes effect in line 2nd (map bentity to bdto), line 1th (aentity to adto map) does not take effect, resulting in the preceding error.
Mapper.initialize (cfg = cfg. Createmap<aentity, adto>= cfg. Createmap<bentity, bdto> ());
Know the reason, the solution is extremely easy, change to the following code can be.
var New mapperconfigurationexpression (); Cfg. Createmap<aentity, adto>(), CFG. Createmap<bentity, bdto>(); Mapper.initialize (CFG);
After this problem is resolved, the AutoMapper upgrade is completed successfully, and then the performance issue of AutoMapper 3.3 is verified to not exist in AutoMapper 5.1.1.
"Missing map" and "Missing type map configuration or unsupported mapping" problem encountered after upgrading AutoMapper