Updating Entities in EntityCollections using Delete() and Add()

Posts   
 
    
tedh
User
Posts: 34
Joined: 14-Dec-2006
# Posted on: 09-Jan-2007 04:34:33   

I want to update an employee record in my SQL 2005 database which is already in my employees EntityCollection . The method I am using is as follows:

1) Load the employee data from the database into a new Entity called "modifiedEntity" (this gives me fresh version of the data).

2) Make changes to modifiedEntity and save it back to the database

To update the employees EntityCollection:

3) Find the original employee entity in employees and delete it.

4) Add modifiedEntity to employees.

Steps (1) and (2) proceed as expected.

In step (3) the old entity is deleted from employees. In the debugger I can still see the record is still in employees, but all of the fields are blanked out and the Fields.State shows as Deleted.

Step (4) fails with the message "This entity is deleted from the database and can't be used in logic.". In the debugger I can see modifiedEntity along with all of the correct field values and Fields.State = New. The original entity record in employees as in step (3).

I don't really understand what is happening here and why this doesn't work?

Thanks

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 09-Jan-2007 08:10:16   

You should Remove the old entity from the collection not Delete it.

If that's the way you are doing it, then please post the complete code snippet.

tedh
User
Posts: 34
Joined: 14-Dec-2006
# Posted on: 09-Jan-2007 16:08:04   

Thank you. Problem resolved.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39927
Joined: 17-Aug-2003
# Posted on: 09-Jan-2007 19:23:01   

tedh wrote:

Thank you. Problem resolved.

Your problem also perfectly illustrates why entitycollection.Remove() doesn't DELETE the entity from the db, it removes it from the collection in memory simple_smile (often users find it odd that entities aren't deleted automatically when the entities are removed from the collection)

Frans Bouma | Lead developer LLBLGen Pro