c# - Inconsistent Mapping Behavior in AutoMapper 3 -


i've had issues automapper 3 mapping related entities entity framework view model of time in mvc 5. i'm using ef 6.1.1 data, , thin , light repository layer centralize queries. issue seems occur mapping properties entities under root entity, problem intermittent. i've seen behavior start working again after rebuilding solution , behavior break after rebuilding solution without changing code issue occurs.

i've tested in debugger , sql profiler, , when data isn't showing on front end, can see in sql profiler entity framework loading related tables.

example mapping issue

this code executes in mvc controller after data has been queried repository layer. view model passed off view rendering.

mapper.createmap<event, eventviewmodel>()     .formember(e => e.eventtitleid, opt => opt.mapfrom(x => x.eventtitletype.eventtitleid)); eventviewmodel model = mapper.map<event, eventviewmodel>(eventitem);  //fix had implement ensure data gets property model.eventtitleid = eventitem.eventtitletype.eventtitleid; 

another example mapping issue

same basic execution path under different controller action, same problem.

mapper.createmap<event, eventviewmodel>()     .formember(l => l.eventtitle, opt => opt.mapfrom(x => x.eventtitletype.eventtitle.title))     .formember(l => l.eventtype, opt => opt.mapfrom(x => x.eventtitletype.lkeventtype.title))     .formember(l => l.locationname, opt => opt.mapfrom(x => x.lklocation.locationname)); ienumerable<eventviewmodel> eventlist = mapper.map<ienumerable<event>, ienumerable<eventviewmodel>>(events); 

again, 3 properties data, , don't.

data model

this data model looks like.

the part of edmx file data model question

repository

the repository layers wraps entity framework calls, not abstract away entity framework entities , issues , benefits has. repositories in project inherit irepository, , i'm using layer centralize query , business logic.

using system.collections.generic;  namespace tools.repository {     /// <summary>     /// generic repository interface     /// </summary>     /// <typeparam name="tclass">class repository implemented for.</typeparam>     public interface irepository<tclass>         tclass : class      {         void savechanges();         tclass find(params object[] keyvalues);         ienumerable<tclass> getall();         tclass add(tclass itemtoadd);         tclass update(tclass itemtoupdate);         tclass remove(tclass itemtodelete);     } } 

summary

i don't believe entity framework lazy loading issue, , after forcing ef eager load data, problem persists. entities have values in debugger before mapping, value mapped view model. frustrating thing issue not seem caused changes i've made view model.

what can cause such inconsistent behavior? i've had problem both automapper 3.2.1 , 3.1.1.

the problem create (configure) mappings right before use them. it's matter of coincidence mapping created first and, thus, prevail.

the automapper documentation says:

if you're using static mapper method, configuration should happen once per appdomain. means best place put configuration code in application startup, such global.asax file asp.net applications.

this means have 1 mapping between event , eventviewmodel, should combination of 2 have now.

by way, can execute occasional mappings dynamicmap.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -