none
Is this approach correct? RRS feed

  • Question

  • Hello !

    I have a situation , where the only way that I have found to delete some objects from entity framework is this :

    context.entry (obj1).State=EntityState.Deleted
    context.entry (obj2).State=EntityState.Deleted
    context.entry (obj3).State=EntityState.Deleted
    context.SaveChanges()

    No matter what is this situation , the only thing that I want to know is that : this approach is correct or no ?

    Thank you !


    • Edited by dcode25 Tuesday, November 11, 2014 6:14 PM
    Tuesday, November 11, 2014 6:13 PM

Answers

  • >>this approach is correct or no ?

    Yes, it is correct. You need to explicitly change the state of a disconnected entity using the DbSet.Entry method like this since the SaveChanged method decides what to do based on the entity’s current state.

    Please remember to mark helpful posts as answer and/or helpful.

    Tuesday, November 11, 2014 10:12 PM
  • Hello dcode25,

    >>So for what I'm asking for , is there any case when this method doesn't work?

    There is a scenario I encountered before is that if you have a couple of mater/detail relationship tables and in database, they are not marked as “ON DELETE CASCADE”, when you use code “context.entry (obj1).State=EntityState.Deleted” to delete a master table record, it does not work because it would throw an exception. You could try it.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 12, 2014 2:59 AM
    Moderator

All replies

  • No matter what is this situation , the only thing that I want to know is that : this approach is correct or no ?

    If it worked for you then why wouldn't it be correct? That's the bottom line, and anything else would be a moot point. 

    Tuesday, November 11, 2014 10:10 PM
  • >>this approach is correct or no ?

    Yes, it is correct. You need to explicitly change the state of a disconnected entity using the DbSet.Entry method like this since the SaveChanged method decides what to do based on the entity’s current state.

    Please remember to mark helpful posts as answer and/or helpful.

    Tuesday, November 11, 2014 10:12 PM
  • Because even the 5/x works in most of the cases , but if x=0 doesn't work.
    So for what I'm asking for , is there any case when this method doesn't work?

    Thank you !

    Wednesday, November 12, 2014 2:29 AM
  • Hello dcode25,

    >>So for what I'm asking for , is there any case when this method doesn't work?

    There is a scenario I encountered before is that if you have a couple of mater/detail relationship tables and in database, they are not marked as “ON DELETE CASCADE”, when you use code “context.entry (obj1).State=EntityState.Deleted” to delete a master table record, it does not work because it would throw an exception. You could try it.

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, November 12, 2014 2:59 AM
    Moderator