none
Keep context alive during entity's lifetime? RRS feed

  • Question

  • Hi all,

    In an EF4, disconnected POCO scenario & winforms, is it safe to create a new objectcontext when instantiating a new class and use this context for loading and CRUD operations?

    Ivan

    Tuesday, May 29, 2012 9:15 AM

Answers

  • Hi Cryo75,

    Based on this scenario, Self-Tracking Entities is sensible. Self-tracking entities have several utility methods to do things like AcceptChanges and StartTracking which work over a single entity. It have the ability to record changes on scalar and complex property values, and on reference and collection navigation properties, independently of the Entity Framework.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Cryo75 Thursday, May 31, 2012 7:09 AM
    Thursday, May 31, 2012 7:04 AM
    Moderator

All replies

  • Hi Ivan, 

    what you mean by "safe"? However the Unit of Work pattern already work in the same manner

    Hope it helps!

    Tuesday, May 29, 2012 11:56 AM
  • By "safe" I mean that it is a recommended way to keep the context alive.

    My business layer has entities with CRUD methods. Each method creates a local context. my entity's Load method does the same. However, I need to change this so that the context stays alive till the entity goes out of scope, because if, for example, I load an entity, do changes to it and save it back to the db, I cannot track changes because it's loaded and saved by two different contexts.

    Tuesday, May 29, 2012 12:00 PM
  • You can detect changes: http://msdn.microsoft.com/en-us/library/dd456848.aspx

    BTW disconnected scenario, when context has short life time is absolutelly normal.

    Tuesday, May 29, 2012 1:08 PM
  • The samples provided for proxy and snapshot show that the changes are done with the context. In my situation I have context per request (even though using winforms) and the contexts won't be the same.
    Wednesday, May 30, 2012 6:07 AM
  • Hi Cryo75,

    Based on this scenario, Self-Tracking Entities is sensible. Self-tracking entities have several utility methods to do things like AcceptChanges and StartTracking which work over a single entity. It have the ability to record changes on scalar and complex property values, and on reference and collection navigation properties, independently of the Entity Framework.

    Best Regards


    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Cryo75 Thursday, May 31, 2012 7:09 AM
    Thursday, May 31, 2012 7:04 AM
    Moderator