New Posts New Posts RSS Feed: Saving a deleted entity
  FAQ FAQ  Forum Search   Calendar   Register Register  Login Login

Saving a deleted entity

 Post Reply Post Reply
Author
Customer View Drop Down
Senior Member
Senior Member
Avatar
User Submitted Questions to Support

Joined: 30-May-2007
Location: United States
Posts: 260
Post Options Post Options   Quote Customer Quote  Post ReplyReply Direct Link To This Post Topic: Saving a deleted entity
    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?

Back to Top
davidklitzke View Drop Down
IdeaBlade
IdeaBlade
Avatar

Joined: 14-Jun-2007
Posts: 715
Post Options Post Options   Quote davidklitzke Quote  Post ReplyReply Direct Link To This Post 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);

 

 

 

Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down