Automapper: Mapping complex object to Entity Framework does not save Changes

Created on 22 Sep 2016  Â·  8Comments  Â·  Source: AutoMapper/AutoMapper

This code does not save references to complex objects / does not save child object changes.

var partner = _mapper.Map<PARTNER>(item); // the partner objects have complex objects
_context.Set<PARTNER>().Attach(partner );
_context.Entry<PARTNER>(part).State = EntityState.Modified;
_context.SaveChanges();

Does there exist a solution on how to map and (!) save complex objects using Entity Framework without coding each child object.?

Most helpful comment

@Jenan I found a solution.
Instead of using:
// query to get the entity here
// then map data like this
entity = _mapper.Map(viewModel);

you just need to change the mapping data like this:
_mapper.Map(viewModel, entity);

and you just call dbContext.SaveChanges(), no need to attach entity anymore.

All 8 comments

Sure! Just not using AutoMapper, I don't reverse map, it gets too
complicated.

On Thursday, September 22, 2016, HJLuecking [email protected]
wrote:

This code does not save references to complex objects / does not save
child object changes.

var partner = _mapper.Map(item); // the partner objects have
complex objects
_context.Set().Attach(partner );
_context.Entry(part).State = EntityState.Modified;
_context.SaveChanges();

Does there exist a solution on how to map and (!) save complex objects
using Entity Framework without coding each child object.?

—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/AutoMapper/AutoMapper/issues/1695, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAGYMmcOZbxeIdqqEr3iLXXTmglnkw_Wks5qsmqNgaJpZM4KDzOb
.

AutoMapper.Collection can handle saving child objects, but complex objects are always a problem

@jbogard You mentioned - don't use automapper - but how solve mapping before updating an entity?
Thank you.

@Jenan I found a solution.
Instead of using:
// query to get the entity here
// then map data like this
entity = _mapper.Map(viewModel);

you just need to change the mapping data like this:
_mapper.Map(viewModel, entity);

and you just call dbContext.SaveChanges(), no need to attach entity anymore.

@chkien0911 Thank you for your post. Is it required the entity load before update? How do you do it?

@Jenan yes, you have to get the entity by its id first
then map the data like this
_mapper.Map(viewModel, entity);
Finally, just call SaveChanges, no need to atttach & set state since DB has been tracking the changes.

@Jenan @chkien0911
https://github.com/AutoMapper/AutoMapper.Collection#what-about-comparing-to-a-single-existing-entity-for-updating

This will find and update the object for you by primary key from EF. Also will add entity if it doesn't exist, so less lines of code needed.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

majidsoltani picture majidsoltani  Â·  5Comments

AndyAyersMS picture AndyAyersMS  Â·  5Comments

Hamidnch picture Hamidnch  Â·  3Comments

WellspringCS picture WellspringCS  Â·  5Comments

dmdymov picture dmdymov  Â·  3Comments