Home All Groups Group Topic Archive Search About

Entity Framework - Reassigning child entity's parent

Author
3 Jul 2009 11:57 PM
Norm
The following seems like a simple enough operation, but for some
reason I can't figure it out.

In a parent-child relationship I want to move a child to a different
parent. I figured that it would be as easy as
"Parent.ChildCollection.Attach(ChildToTransfer)" but it doesn't work.
All of the entities are attached to the same object context and
already exist in the database. No inserting here, just updating.

At the time that ".SaveChanges()" is called on the container, the
states of the various entities are thus:
Parent.EntityState = Modified (Due to other code)
Parent.ChildCollection contains the ChildToTransfer
Child.Parent = The new Parent (correct)
BUT Child.EntityState = Unchanged

The parent gets updated, but the child record doesn't.

Any help would be appreciated and thanks in advance.

- Norm

Author
4 Jul 2009 12:38 AM
Mr. Arnold
Show quote Hide quote
"Norm" <neon***@gmail.com> wrote in message
news:74735735-6430-47c6-b29c-65091d724647@v23g2000pro.googlegroups.com...
> The following seems like a simple enough operation, but for some
> reason I can't figure it out.
>
> In a parent-child relationship I want to move a child to a different
> parent. I figured that it would be as easy as
> "Parent.ChildCollection.Attach(ChildToTransfer)" but it doesn't work.
> All of the entities are attached to the same object context and
> already exist in the database. No inserting here, just updating.
>
> At the time that ".SaveChanges()" is called on the container, the
> states of the various entities are thus:
> Parent.EntityState = Modified (Due to other code)
> Parent.ChildCollection contains the ChildToTransfer
> Child.Parent = The new Parent (correct)
> BUT Child.EntityState = Unchanged
>
> The parent gets updated, but the child record doesn't.
>
> Any help would be appreciated and thanks in advance.
>

Well, I have over 40 entities on the EF model using a WCF Web service on the
backend that has the BLL and the DAL behind the WCF Web service SOA
solution, used by an ASP.NET UI front-end.

I learned to remove the associations/relationships between entities on the
model, keeping the constants on the SQL tables themselves.

That way, I have complete control of the EF model, no surprises.



__________ Information from ESET NOD32 Antivirus, version of virus signature database 4214 (20090703) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com
Are all your drivers up to date? click for free checkup

Author
6 Jul 2009 5:32 PM
Norm
Show quote Hide quote
On Jul 3, 5:38 pm, "Mr. Arnold" <MR. Arn***@Arnold.com> wrote:
> "Norm" <neon***@gmail.com> wrote in message
>
> news:74735735-6430-47c6-b29c-65091d724647@v23g2000pro.googlegroups.com...
>
>
>
>
>
> > The following seems like a simple enough operation, but for some
> > reason I can't figure it out.
>
> > In a parent-child relationship I want to move a child to a different
> > parent. I figured that it would be as easy as
> > "Parent.ChildCollection.Attach(ChildToTransfer)" but it doesn't work.
> > All of the entities are attached to the same object context and
> > already exist in the database. No inserting here, just updating.
>
> > At the time that ".SaveChanges()" is called on the container, the
> > states of the various entities are thus:
> > Parent.EntityState = Modified (Due to other code)
> > Parent.ChildCollection contains the ChildToTransfer
> > Child.Parent = The new Parent (correct)
> > BUT Child.EntityState = Unchanged
>
> > The parent gets updated, but the child record doesn't.
>
> > Any help would be appreciated and thanks in advance.
>
> Well, I have over 40 entities on the EF model using a WCF Web service on the
> backend that has the BLL and the DAL behind the WCF Web service SOA
> solution, used by an ASP.NET UI front-end.
>
> I learned to remove the associations/relationships between entities on the
> model, keeping the constants on the SQL tables themselves.
>
> That way, I have complete control of the EF model, no surprises.
>
> __________ Information from ESET NOD32 Antivirus, version of virus signature database 4214 (20090703) __________
>
> The message was checked by ESET NOD32 Antivirus.
>
> http://www.eset.com- Hide quoted text -
>
> - Show quoted text -

Mr. Arnold,

While that is a valid answer, I am not in a posistion where I can redo
the model in such a way.  After running into this and seeing your
answer (and the lack of anyone elses), I am inclined to not use EF in
the future. However, I am still in need of a solution for my current
project.

- Norm
Author
6 Jul 2009 7:28 PM
Norm
On Jul 6, 10:32 am, Norm <neon***@gmail.com> wrote:
Show quoteHide quote
> On Jul 3, 5:38 pm, "Mr. Arnold" <MR. Arn***@Arnold.com> wrote:
>
>
>
>
>
> > "Norm" <neon***@gmail.com> wrote in message
>
> >news:74735735-6430-47c6-b29c-65091d724647@v23g2000pro.googlegroups.com....
>
> > > The following seems like a simple enough operation, but for some
> > > reason I can't figure it out.
>
> > > In a parent-child relationship I want to move a child to a different
> > > parent. I figured that it would be as easy as
> > > "Parent.ChildCollection.Attach(ChildToTransfer)" but it doesn't work.
> > > All of the entities are attached to the same object context and
> > > already exist in the database. No inserting here, just updating.
>
> > > At the time that ".SaveChanges()" is called on the container, the
> > > states of the various entities are thus:
> > > Parent.EntityState = Modified (Due to other code)
> > > Parent.ChildCollection contains the ChildToTransfer
> > > Child.Parent = The new Parent (correct)
> > > BUT Child.EntityState = Unchanged
>
> > > The parent gets updated, but the child record doesn't.
>
> > > Any help would be appreciated and thanks in advance.
>
> > Well, I have over 40 entities on the EF model using a WCF Web service on the
> > backend that has the BLL and the DAL behind the WCF Web service SOA
> > solution, used by an ASP.NET UI front-end.
>
> > I learned to remove the associations/relationships between entities on the
> > model, keeping the constants on the SQL tables themselves.
>
> > That way, I have complete control of the EF model, no surprises.
>
> > __________ Information from ESET NOD32 Antivirus, version of virus signature database 4214 (20090703) __________
>
> > The message was checked by ESET NOD32 Antivirus.
>
> >http://www.eset.com-Hide quoted text -
>
> > - Show quoted text -
>
> Mr. Arnold,
>
> While that is a valid answer, I am not in a posistion where I can redo
> the model in such a way.  After running into this and seeing your
> answer (and the lack of anyone elses), I am inclined to not use EF in
> the future. However, I am still in need of a solution for my current
> project.
>
> - Norm- Hide quoted text -
>
> - Show quoted text -

I have found the problem. It (of course) had nothing to do with the
Entity Framework.
/sigh

Note: "Parent.ChildCollection.Attach(ChildToTransfer)" is not correct.
Use "ChildToTransfer.Parent = NewParent"

- Norm
Author
6 Jul 2009 8:09 PM
Mr. Arnold
"Norm" <neon***@gmail.com> wrote in message
news:2dafade8-02bb-4265-82a4-09ef58176782@j9g2000prh.googlegroups.com...
On Jul 6, 10:32 am, Norm <neon***@gmail.com> wrote:
Show quoteHide quote
> On Jul 3, 5:38 pm, "Mr. Arnold" <MR. Arn***@Arnold.com> wrote:
>
>
>
>
>
> > "Norm" <neon***@gmail.com> wrote in message
>
> >news:74735735-6430-47c6-b29c-65091d724647@v23g2000pro.googlegroups.com...
>
> > > The following seems like a simple enough operation, but for some
> > > reason I can't figure it out.
>
> > > In a parent-child relationship I want to move a child to a different
> > > parent. I figured that it would be as easy as
> > > "Parent.ChildCollection.Attach(ChildToTransfer)" but it doesn't work.
> > > All of the entities are attached to the same object context and
> > > already exist in the database. No inserting here, just updating.
>
> > > At the time that ".SaveChanges()" is called on the container, the
> > > states of the various entities are thus:
> > > Parent.EntityState = Modified (Due to other code)
> > > Parent.ChildCollection contains the ChildToTransfer
> > > Child.Parent = The new Parent (correct)
> > > BUT Child.EntityState = Unchanged
>
> > > The parent gets updated, but the child record doesn't.
>
> > > Any help would be appreciated and thanks in advance.
>
> > Well, I have over 40 entities on the EF model using a WCF Web service on
> > the
> > backend that has the BLL and the DAL behind the WCF Web service SOA
> > solution, used by an ASP.NET UI front-end.
>
> > I learned to remove the associations/relationships between entities on
> > the
> > model, keeping the constants on the SQL tables themselves.
>
> > That way, I have complete control of the EF model, no surprises.
>
> > __________ Information from ESET NOD32 Antivirus, version of virus
> > signature database 4214 (20090703) __________
>
> > The message was checked by ESET NOD32 Antivirus.
>
> >http://www.eset.com-Hide quoted text -
>
> > - Show quoted text -
>
> Mr. Arnold,
>
> While that is a valid answer, I am not in a posistion where I can redo
> the model in such a way. After running into this and seeing your
> answer (and the lack of anyone elses), I am inclined to not use EF in
> the future. However, I am still in need of a solution for my current
> project.
>
> - Norm- Hide quoted text -
>
> - Show quoted text -

I have found the problem. It (of course) had nothing to do with the
Entity Framework.
/sigh

Note: "Parent.ChildCollection.Attach(ChildToTransfer)" is not correct.
Use "ChildToTransfer.Parent = NewParent"


Well, no matter what ORM solution one uses including nHibernate and others,
there is always going to be a learning curve.


__________ Information from ESET NOD32 Antivirus, version of virus signature database 4219 (20090705) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Bookmark and Share