New Posts New Posts RSS Feed: Marking object for deletion and setting reference to new object does not work
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Marking object for deletion and setting reference to new object does not work

 Post Reply Post Reply
Author
rasmus View Drop Down
Groupie
Groupie


Joined: 05-Aug-2009
Location: Denmark
Posts: 63
Post Options Post Options   Quote rasmus Quote  Post ReplyReply Direct Link To This Post Topic: Marking object for deletion and setting reference to new object does not work
    Posted: 23-Sep-2013 at 11:52am
Hi

I have noticed what I find a strange behavior. When first marking an object for deletion and adding anothor object in its place. At this point, the ParentEntity and the ChildEntity are loaded and in the EntityCache:

ParentEntity.ChildEntity.EntityAspect.Delete(); // mark the child entity for deletion

var childEntity = new ChildEntity();     //create a new
ChildEntity

manager.AddEntity(childEntity);     //Add to the entityManager
          
ParentEntity.ChildEntity = childEntity;     //set the reference to the childEntity

await manager.SaveChangesAsync();//save the changes

Now I would expect the ParentEntity.ChildEntity to have a reference
to the same object as the variable childEntity, but this is not the case.
After the save, the ParentEntity.ChildEntity holds a reference to a null-entity and this entity has the EntityState Detached.

If I remove the first line ParentEntity.ChildEntity.EntityAspect.Delete();
everything works as expected, and after SaveChangesAsync, the ParentEntity.ChildEntity holds a referece to the same object as the variable childEntity and the EntityState has the state of Unchanged

Why does the behavior change because of the first line?

Best regards,
Rasmus
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 23-Sep-2013 at 1:04pm
Hi Rasmus,

What is the relationship between ParentEntity and ChildEntity?

Based on your description, I assume it would be Parent-1_0..1-Child, but in that case it would not make sense to call Delete and then create another Child entity and add to Parent because the Deleted entity still exists and the new Child entity would have the same PK (on a one to one relationship, the FK is the PK). 
As a matter of fact, I'd get an exception for duplicate keys when trying to associate the newChild to Parent.

Please provide enough detail so we can try to reproduce the problem.
Back to Top
rasmus View Drop Down
Groupie
Groupie


Joined: 05-Aug-2009
Location: Denmark
Posts: 63
Post Options Post Options   Quote rasmus Quote  Post ReplyReply Direct Link To This Post Posted: 24-Sep-2013 at 9:33am
Hi

I do not get a duplicate key exception, as the primary key is an identity column. So after the new childEntity is added to the manager, the id is -101 and after the SaveChangesAsync, a new Id is generated for the childEntity.

So what behavior would be the correct one to expect after the save? Should the ParentEntity.ChildEntity hold a reference to a null Entity or should it hold a reference to the newly created child?

Best regards,
Rasmus
Back to Top
sbelini View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 13-Aug-2010
Location: Oakland
Posts: 786
Post Options Post Options   Quote sbelini Quote  Post ReplyReply Direct Link To This Post Posted: 24-Sep-2013 at 9:39am
Can you provide a small sample reproducing the problem?

There must be something in your model different than mine. The simplest way to find the issue is by having a small sample reproducing the issue.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down