c# - AutoMapper - AutoMapper.AutoMapperMappingException -


i'm having exception when i'm trying map 1 object another.

on global.asax.cs got this:

rolemanager<identityrole> rolemanager = new rolemanager<identityrole>(new rolestore<identityrole>(new appcontext()));         mapper.initialize(cfg =>         {             cfg.createmap<appuser, tokenauthorizationmodel>()             .formember(dest => dest.username, opt => opt.mapfrom(src => src.username))             .formember(dest => dest.role, opt => opt.mapfrom(src => rolemanager.findbyid(src.roles.first().roleid).name));         }); 

and got automapper.automappermappingexception exception on login controller, especifically on line:

tokenauthorizationmodel tokenclaims = mapper.map<tokenauthorizationmodel>(validuser); 

and these models:

public class appuser : identityuser {     public virtual list<coursemodel> courses { get; set; }     public string fullname { get; set; }     public int reputation { get; set; } } 

and destination:

public class tokenauthorizationmodel {     public string username { get; set; }     public string role { get; set; } } 

can give me hand? in advance! :)

i think problem @ part:

.formember(dest => dest.role, opt => opt.mapfrom(src => rolemanager.findbyid(src.roles.first().roleid).name)); 

i not use mapfrom in case. try use resolveusing method instead takes lambda function.

.formember(dest => dest.role, opt => opt.resolveusing(src => rolemanager.findbyid(src.roles.first().roleid).name)); 

if not work please let me know!


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -