locked
Cannot delete an entity RRS feed

  • Question

  • User1536465747 posted
    for(Int32 i = 0; i < newObj.Skills.Count; i++){
    Skill objSkill = newObj.Skills.ElementAt(i);
    //newObj.Skills.Remove(objSkill); this does not work either
    context.Detach(objSkill);
    }


    I want to remove the existing Skills but obviously it does not work as expected. How could i delete the skills before i add new ones (the addSkill method works fine but it keeps adding new skills to the existing list though i want to empty previously)

    Thank you 

    P.S. i don't get any error but the new skills are added to the list of items that i expect to be removed previously.

    Wednesday, March 18, 2015 5:48 AM

Answers

  • User1689970273 posted

    Can you post your entities that have relationship with Skills? I'm assuming that skill is another table from a parent table.

    If you are unsure, you may do the following...

      DatabaseContext.Entry(entity).State = EntityState.Deleted;

    before you call the context.Savechanges.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 18, 2015 10:11 AM

All replies

  • User1689970273 posted

    Can you post your entities that have relationship with Skills? I'm assuming that skill is another table from a parent table.

    If you are unsure, you may do the following...

      DatabaseContext.Entry(entity).State = EntityState.Deleted;

    before you call the context.Savechanges.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 18, 2015 10:11 AM
  • User61956409 posted

    Hi KulerMaster,

    Thanks for your post.

    Please ensure the SaveChanges method is called to write changes to the database.

    context.SaveChanges();

    Best Regards,

    Fei Han

    Friday, March 20, 2015 2:30 AM
  • User1536465747 posted

    Actually that's where the problem lies. I call the save changes method but anyway i have only new skills added without old ones detached. 

    Wednesday, April 8, 2015 7:11 AM