AutoMapper.Mapper.CreateMap <TSource, TDestination> () 'deprecated

I have Like classes

class A { public int id {get; set;} } class B { public C c {get; set;} } class C { public int id {get; set;} public string Name {get; set;} } 

My requirement is to match the identifier of class A with the id of class C. Now I have done so far: Mapper.CreateMap (). ForMember (des => des.C.Id, src => src.MapFrom (x => x.id));

and it worked fine.

Now it seems that Auto mapper has changed its implementation. and I get a warning as below:

AutoMapper.Mapper.CreateMap () 'deprecated: "Dynamic map creation will be removed in version 5.0. Use the MapperConfiguration instance and statically save it as needed or Mapper.Initialize. Use CreateMapper to create the mapper instance.

I need to map some class properties that have a different name and structure. Any help on this.

+8
c # asp.net-mvc-4 automapper automapper-3
source share
3 answers

Earlier

  Mapper.CreateMap<Src, Dest>() .ForMember(d => d.UserName, opt => opt.MapFrom(/* ????? */)); 

The problem here in the definition of the mapping is static, defined once, and reused throughout the life of the application. Before 3.3, you will need to override the mapping for each query with a hard-coded value. And since the mapping configuration is created in a separate place, in addition to our display execution, we need to somehow introduce the run-time parameter into our configuration, and then provide it at runtime.

This is done in two parts: the definition of the display in which we create the run-time parameter, and then at run time, when we deliver it. To create a display definition with the runtime parameter, we "fake" a closure that includes a named local variable:

 Mapper.Initialize(cfg => { string userName = null; cfg.CreateMap<Source, Dest>() .ForMember(d => d.UserName, opt => opt.MapFrom(src => userName) ); }); 

For more information see this

For one or more classes

  cfg.CreateMissingTypeMaps = true; cfg.CreateMap<Source, Dest>() .ForMember(d => d.UserName, opt => opt.MapFrom(src => userName) ); cfg.CreateMap<AbcEditViewModel, Abc>(); cfg.CreateMap<Abc, AbcEditViewModel>(); }); 

In the display class

  IMapper mapper = config.CreateMapper(); var source = new AbcEditViewModel(); var dest = mapper.Map<AbcEditViewModel, Abct>(source); 
+16
source share

Finally, I found a resolution. I did: Mapper.Initialize{ Mapping field from source to destination } in App_start and adding this file to global.asax → Application_Start () → GlobalConfiguration.

I need to add another line inside my Mapper.Initialize, which is cfg.CreateMissingTypeMaps = true;

Now this code will work for explicit matching, where the two classes do not have the same structure and property names.

In addition, if we need to display the properties of two classes with the same structure, the Mapper.map(source, destination) code, which did not work before, will work.

Let me know if anyone has difficulty resolving. Thanks to everyone for the above answer.

0
source share

Another way that seems a little cleaner is to create the MappingProfile class, which inherits from the Profile AutoMapper class

 public class MappingProfile:Profile { public MappingProfile() { CreateMap<Source1, Destination1>(); CreateMap<Source2, Destination2>(); ... } } 

Then you initialize the mapping using Mapper.Initialize(c => c.AddProfile<MappingProfile>()); in your starter code

This will allow you to use the display anywhere, causing

 destination1Collection = source1Collection.Select(Mapper.Map<Source1, Destination1>); 
0
source share

All Articles