riteshrao/ncommon — Issue #32: Attaching "nested" models
Author: marvealmar
Created: 2012-01-23 12:27:30+00:00
State: open

Hi,

I have a service layer from with I pull some data in a UoW scope. I do some eager fetching the retrieve associated data. Then i perform actions on this model object, change data, adding new entities to an association collection etc. My problem is when I send the changed object back for processing in my service layer. I had some proplem updating the object, getting an error saying something like that the object already exist in the ObjectStateManager. What I did is attaching the "root" object. However this did not result in an update/save of changed properties in the child object. I had to loop each child association and then attach them the their own reposiotory. Something like this:

using (var scope = new UnitOfWorkScope())
{
var rootDomainRepository = new EFRepository();
var childRepos = new EFRepository();
myrootobject.SomeMethod() //argument in service method
myrootobject.Children.ForEach( c => childRepos.Attach(c));
rootDomainRepository.Attach(myrootobject);
scope.Commit();
}

Now, is this the way to do it? Would it be possible to add support for attaching children/association when attaching a parent object? Or am I doing something fundementally wrong:)