c# - Using Automapper static API in the latest release? -
i've upgraded automapper
4.2.1 5.0.0. i'm using static api in webapi2 project , i'm trying mapping work, tried following this answer.
so changed code following:
public static class automapping { public static void config() { mapper.initialize(main => { var config = new mapperconfiguration(cfg => { cfg.createmissingtypemaps = true; cfg.createmap<mymodel, mydto>().reversemap(); }); config.assertconfigurationisvalid(); }); } }
the above called global.asax
.
however, exception:
mapper not initialized. call initialize appropriate configuration.
what correct way initialize automapper
, , need change controllers mapping?
edit1
firstly, code above must be:
mapper.initialize(cfg => { cfg.createmissingtypemaps = true; cfg.createmap<mymodel, mydto>().reversemap(); }); mapper.configuration.assertconfigurationisvalid();
secondly, problem might in following method use ignore missing properties:
public static imappingexpression<tsource, tdestination> ignoreunmapped<tsource, tdestination>(this imappingexpression<tsource, tdestination> expression) { var typemap = mapper.configuration.findtypemapfor<tsource, tdestination>(); if (typemap != null) { foreach (var unmappedpropertyname in typemap.getunmappedpropertynames()) { expression.formember(unmappedpropertyname, opt => opt.ignore()); } } return expression; }
i'm assuming 'mapper.configuration' not yet configured because above method called within initialize
configures mapping.
is there existing method within automapper can use instead of above?
edit2
would following syntax work?
cfg.createmap<mymodel, mydto>().reversemap().forallmembers(opt => opt.ignore());
actually code nothing now. have change this:
public static class automapping { public static void config() { mapper.initialize(cfg => { cfg.createmissingtypemaps = true; cfg.createmap<mymodel, mydto>().reversemap(); }); mapper.assertconfigurationisvalid(); } }
upd (after edit1):
try use expression.typemap
instead of mapper.configuration.findtypemapfor<tsource, tdestination>()
Comments
Post a Comment