"Missing map" and "Missing type map configuration or unsupported mapping" problem encountered after upgrading AutoMapper

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.