none
How EF AddOrUpdate works RRS feed

  • Question

  • please provide some sample code just to show how EF AddOrUpdate works ?

    suppose i have list of customer data some of data will be inserted in db and some of data will be updated in db. just show me how to do it by EF AddOrUpdate works with sample code. thanks

    Monday, May 21, 2018 6:48 PM

Answers

  • Hi Sudip_inn,

    >>suppose i have list of customer data some of data will be inserted in db and some of data will be updated in db. just show me how to do it by EF AddOrUpdate works with sample code. thanks

    According to your description, It seems that you could change related customer entity's states, then execute SaveChanges(), then entity framework will insert or update via entities state.

    An entity can be in one of five states as defined by the EntityState enumeration. These states are:

    • Added: the entity is being tracked by the context but does not yet exist in the database
    • Unchanged: the entity is being tracked by the context and exists in the database, and its property values have not changed from the values in the database
    • Modified: the entity is being tracked by the context and exists in the database, and some or all of its property values have been modified
    • Deleted: the entity is being tracked by the context and exists in the database, but has been marked for deletion from the database the next time SaveChanges is called
    • Detached: the entity is not being tracked by the context

    SaveChanges does different things for entities in different states:

    • Unchanged entities are not touched by SaveChanges. Updates are not sent to the database for entities in the Unchanged state.
    • Added entities are inserted into the database and then become Unchanged when SaveChanges returns.
    • Modified entities are updated in the database and then become Unchanged when SaveChanges returns.
    • Deleted entities are deleted from the database and are then detached from the context.

    For more information, please refer to:

    https://msdn.microsoft.com/en-us/library/jj592676(v=vs.113).aspx

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Sunday, May 27, 2018 6:57 PM
    Tuesday, May 22, 2018 5:19 AM
    Moderator

All replies

  • Hi Sudip_inn,

    >>suppose i have list of customer data some of data will be inserted in db and some of data will be updated in db. just show me how to do it by EF AddOrUpdate works with sample code. thanks

    According to your description, It seems that you could change related customer entity's states, then execute SaveChanges(), then entity framework will insert or update via entities state.

    An entity can be in one of five states as defined by the EntityState enumeration. These states are:

    • Added: the entity is being tracked by the context but does not yet exist in the database
    • Unchanged: the entity is being tracked by the context and exists in the database, and its property values have not changed from the values in the database
    • Modified: the entity is being tracked by the context and exists in the database, and some or all of its property values have been modified
    • Deleted: the entity is being tracked by the context and exists in the database, but has been marked for deletion from the database the next time SaveChanges is called
    • Detached: the entity is not being tracked by the context

    SaveChanges does different things for entities in different states:

    • Unchanged entities are not touched by SaveChanges. Updates are not sent to the database for entities in the Unchanged state.
    • Added entities are inserted into the database and then become Unchanged when SaveChanges returns.
    • Modified entities are updated in the database and then become Unchanged when SaveChanges returns.
    • Deleted entities are deleted from the database and are then detached from the context.

    For more information, please refer to:

    https://msdn.microsoft.com/en-us/library/jj592676(v=vs.113).aspx

    Best regards,

    Zhanglong


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Sudip_inn Sunday, May 27, 2018 6:57 PM
    Tuesday, May 22, 2018 5:19 AM
    Moderator
  • i asked this ---- suppose i have list of customer data some of data will be inserted in db and some of data will be updated in db. just show me how to do it by EF AddOrUpdate works with sample code. thanks

    if possible sir please explain this with a code example. thanks

    • Marked as answer by Sudip_inn Sunday, May 27, 2018 6:56 PM
    • Unmarked as answer by Sudip_inn Sunday, May 27, 2018 6:56 PM
    Sunday, May 27, 2018 6:53 PM