Print Page | Close Window

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

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce 2012
Forum Discription: For .NET 4.5
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=4338
Printed Date: 30-May-2025 at 7:35pm


Topic: Marking object for deletion and setting reference to new object does not work
Posted By: rasmus
Subject: Marking object for deletion and setting reference to new object does not work
Date 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



Replies:
Posted By: sbelini
Date 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.


Posted By: rasmus
Date 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


Posted By: sbelini
Date 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.



Print Page | Close Window