none
Reuse / Create New Data Context

    Question

  • Hi,

    I am using LINQ as a connection base for my project.
    Initially, i set up my dbcontext as a global variable, to be used by all pages in project, but i found that, in that case, it is really hard to get the latest data. I need to refresh the source every time when i need the latest source.

    So i am thinking to create new context for every page, but i face error with

    "Cannot access a disposed object."

    Or

    "An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext.  This is not supported."

    Can you give me some reference what is the proper way to create/use dbcontext? Almost every page in my project requires to get latest data form database, most of them are grid,  allow CRUD as well. Some of them are static variable like storing CurrentUser.

    I am doing .net WIN and WEB project now. Thx,

    Friday, October 04, 2013 7:28 AM

Answers

All replies

  • Hello,

    Thanks for posting to this forum.

    If I understand correctly, you want to know how to create/use dbcontext.

    For this, Rick Strahl has a very helpful and detailed article discussing this topic.  In this article he concluded four general approaches to manage the DataContext lifetime, but each one has its merits, drawbacks, and suitable scenarios.

    1. Create a new Context for each atomic operation (Application level management)
    2. Create a global DataContext and handle all operations against this single DataContext object
    3. Create a thread specific DataContext
    4. Create a per business object DataContext

    For detail, please see LINQ to SQL DataContext Lifetime Management by Rick Strahl.

    Another great article on this topic is Dinesh Kulkarni’s Lifetime of LINQ to SQL DataContext.  This article can tell us why DataContext is not encouraged to be kept for a long time.

    And actually Microsoft provides the following advice/recommendation to not reuse DataContext instances.

    http://msdn.microsoft.com/en-us/library/bb386929.aspx

    Frequently Asked Questions (LINQ to SQL)

    Connection Pooling

    Q. Is there a construct that can help with DataContext pooling?

    A. Do not try to reuse instances of DataContext. Each DataContext maintains state (including an identity cache) for one particular edit/query session. To obtain new instances based on the current state of the database, use a new DataContext.

    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.

    Monday, October 07, 2013 1:52 AM
    Moderator
  • Hi,

    Let's say i decide to create new context for every page, but i face error with "Cannot access a disposed object."

    Or

    "An attempt has been made to Attach or Add an entity that is not new, perhaps having been loaded from another DataContext.  This is not supported."

    May i know how to solve this issue? and what is the root cause?
    Wednesday, October 09, 2013 4:24 AM
  • The DataContext is the source of all entities mapped over a database connection. It tracks changes that you made to all retrieved entities and maintains an "identity cache" that guarantees that entities retrieved more than one time are represented by using the same object instance.

    In general, a DataContext instance is designed to last for one "unit of work" however your application defines that term. A DataContext is lightweight and is not expensive to create. A typical LINQ to SQL application creates DataContext instances at method scope or as a member of short-lived classes that represent a logical set of related database operations.

    So re-using a DataContext is not a best practice.

    See it:http://stackoverflow.com/questions/3233839/why-would-reusing-a-datacontext-have-a-negative-performance-impact

    Wednesday, October 23, 2013 5:32 AM