Print Page | Close Window

Saving a deleted entity

Printed From: IdeaBlade
Category: DevForce
Forum Name: DevForce Classic
Forum Discription: For .NET 2.0
URL: http://www.ideablade.com/forum/forum_posts.asp?TID=359
Printed Date: 01-May-2025 at 7:14pm


Topic: Saving a deleted entity
Posted By: Customer
Subject: Saving a deleted entity
Date Posted: 08-Aug-2007 at 12:16pm

I am trying to save one entity only.

I tred to use: _msManager.SaveChanges(myentity), but that does not work.

I tried : _msManager.SaveChanges(myentity.ToArray()), but that does not work with deleted rows.

Do you have a suggestion?




Replies:
Posted By: davidklitzke
Date Posted: 08-Aug-2007 at 12:19pm

The standard way to save just a few entities is to populate an EntityList with only the entities that you want to save, and then call:

 

SaveChanges(AnEntityList);

 

Where AnEntityList is an instance on an EntityList.

 

The thing that is a little tricky is saving a deleted entity  (i.e., deleting an entity from the database).  If you  delete an entity, then populate an EntityList with the deleted entity, and finally call SaveChanges on the EntityList, you will find that the entity does not get deleted from the database.  The reason for this is that the default behavior of an EntityList is to remove any deleted entities.  To override this behavior, set the ShouldRemoveDeletedEntities property of the EntityList to false.

 

Here is a code sample that will delete the currently selected Employee from the database:

 

      Employee aEmployee = (Employee)mEmpSource.Current;

      EntityList<Employee> aDeletedEmployeeList = new EntityList<Employee>();

      aDeletedEmployeeList.Add(aEmployee);

      aDeletedEmployeeList.ShouldRemoveDeletedEntities = false;

      aEmployee.Delete();

      mPM.SaveChanges(aDeletedEmployeeList);

 

 

 




Print Page | Close Window