locked
How to delete objects that were just added RRS feed

  • Question

  • Hi

     

     

    Help me with my scenario:

    1)Create and add new entity to some collection which is already in objectcontext.

    2)Remove this new entity

    3)call SaveChanges()

    4)get an exception

    The reason for exception is this new entity has null reference after removing and its EntityState is "Added" - so objectcontext tries to add this entity but encounters "not null" constraint in database.

     To overcome this situation i can manually set EntityState to "Detached" to this entity after removing, so objectcontext will not try to add it. But this is nightmarish workaround.

    Is there any good way for this scenario?

     

     

     

     

     

     

    Thursday, July 22, 2010 10:00 AM

Answers

  • Hi,

    Even though the entity isn't persisted to the database, in order to remove it from the context you can use the DeleteObject method of the ObjectContext.

    I hope it will help you.


    Gil Fink
    • Proposed as answer by liurong luo Friday, July 23, 2010 8:20 AM
    • Marked as answer by DimaSfR Wednesday, July 28, 2010 7:14 AM
    Thursday, July 22, 2010 12:41 PM
  • Gil is right. I do intent to right a blog post on this soon if someone has not written one. This question has been asked so many times on the forum and the answer is always same. imagine the scenario of order and orderdetails. Say you create an orderdetail like below and add it to the OrderDetails collection of order entity.

    var orderdetail = new OrderDetail{};

    order.OrderDetails.AddObject(orderdetail);

    //now remove order detail 

    db.OrderDetails.DeleteObject(orderdetail);

    db.SaveChanges();

    You have to understand what i just did as compared to what u were saying. When you do order.OrderDetails.Remove(orderdetail) you merely removing the orderdetail from the OrderDetails collection but then that OrderDetail still exist in the ObjectContext and is not pointed to any order. So when you call SaveChanges and u are using foreign key association, database will throw an exception saying that it cant insert the OrderDetail because it does not know what Order it belongs to. 

    Removing is not same as deleting it. What u are essentially wanting to do is delete the OrderDetail which does two things. It marks the entity in deleted state and also detaches the orderdetail from the order.OrderDetails Collection because a deleted entity cannot be part of a collection. 


    Zeeshan Hirani Entity Framework 4.0 Recipes by Apress
    http://weblogs.asp.net/zeeshanhirani
    • Proposed as answer by liurong luo Friday, July 23, 2010 8:20 AM
    • Marked as answer by DimaSfR Wednesday, July 28, 2010 7:14 AM
    Thursday, July 22, 2010 5:46 PM

All replies

  • Hi,

    Even though the entity isn't persisted to the database, in order to remove it from the context you can use the DeleteObject method of the ObjectContext.

    I hope it will help you.


    Gil Fink
    • Proposed as answer by liurong luo Friday, July 23, 2010 8:20 AM
    • Marked as answer by DimaSfR Wednesday, July 28, 2010 7:14 AM
    Thursday, July 22, 2010 12:41 PM
  • Gil is right. I do intent to right a blog post on this soon if someone has not written one. This question has been asked so many times on the forum and the answer is always same. imagine the scenario of order and orderdetails. Say you create an orderdetail like below and add it to the OrderDetails collection of order entity.

    var orderdetail = new OrderDetail{};

    order.OrderDetails.AddObject(orderdetail);

    //now remove order detail 

    db.OrderDetails.DeleteObject(orderdetail);

    db.SaveChanges();

    You have to understand what i just did as compared to what u were saying. When you do order.OrderDetails.Remove(orderdetail) you merely removing the orderdetail from the OrderDetails collection but then that OrderDetail still exist in the ObjectContext and is not pointed to any order. So when you call SaveChanges and u are using foreign key association, database will throw an exception saying that it cant insert the OrderDetail because it does not know what Order it belongs to. 

    Removing is not same as deleting it. What u are essentially wanting to do is delete the OrderDetail which does two things. It marks the entity in deleted state and also detaches the orderdetail from the order.OrderDetails Collection because a deleted entity cannot be part of a collection. 


    Zeeshan Hirani Entity Framework 4.0 Recipes by Apress
    http://weblogs.asp.net/zeeshanhirani
    • Proposed as answer by liurong luo Friday, July 23, 2010 8:20 AM
    • Marked as answer by DimaSfR Wednesday, July 28, 2010 7:14 AM
    Thursday, July 22, 2010 5:46 PM
  • Thanks a lot, that works
    Wednesday, July 28, 2010 7:14 AM