Automapper : mapping issue with inheritance and abstract base class on collections with Entity Framework 4 Proxy Pocos

后端 未结 5 835
轻奢々
轻奢々 2020-12-07 20:23

I am having an issue using AutoMapper (which is an excellent technology) to map a business object to a DTO where I have inheritance off of an abstract base class within a co

5条回答
  •  攒了一身酷
    2020-12-07 21:22

    I've just faced the same problem with mapping dynamic EF proxies to ViewModels in MVC application.

    I found an easy solution using Mapper.DynamicMap() for this problem. Here is my code:

    Converting from Dynamic proxy to ViewModel class:

    // dynamic proxy instance
    WebService webService = _repWebService.GetAll().SingleOrDefault(x => x.Id == id);
    
    //mapping
    FirstStepWebServiceModel model = Mapper.DynamicMap(webService);
    

    Converting from ViewModel class to EF Dynamic Proxy:

    [HttpPost]
    public ActionResult FirstStep(FirstStepWebServiceModel input)
    {
        // getting the dynamic proxy from database
        WebService webService = _repWebService.GetAll().Single(x => x.Id == input.WebServiceId);
    
        // mapping the input ViewModel class to the Dynamic Proxy entity
        Mapper.DynamicMap(input, webService);
    }
    

    Hope this example help you

提交回复
热议问题