Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3232876
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T17:12:43+00:00 2026-05-17T17:12:43+00:00

I have a class that uses linq to access the database. Some methods call

  • 0

I have a class that uses linq to access the database. Some methods call others. For example:

class UserManager
{
   public User[] getList()
   {
       using(var db = new MyContext())
       {
             return db.Users.Where(item => item.Active == false);
       }
    }
    public User[] addUser(string name)
    {
       using(var db = new MyContext())
       {
           db.Users.InsertOnSubmit(new User() { id = Guid.NewId(),  name = name, active = false ...});
       }
       return getList();
    }

…

In the call to addUser I am required to return the new list. (I know as it stands it isn’t a great design, but I have eliminated detail for simplicity.) However, the call to getList creates a second data context.

I could pad this out with extra methods, viz:

public getList()
{
     using(var db = new MyContext())
        return getList(db);
}
public getList(MyContext db)
{
      ...
}

Then replace my call in addUser so as to keep the same data context.

I seem to see this type of thing a lot in my code, and I am concerned with the cost of creating and releasing all these data contexts. Does anyone have an opinion as to whether it is worthwhile putting in the extra work to eliminate the creation and deletion of these contexts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T17:12:44+00:00Added an answer on May 17, 2026 at 5:12 pm

    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.

    You can still use underlying ADO.NET
    connection pooling. For more
    information, see SQL Server Connection
    Pooling (ADO.NET).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class 'Data' that uses a getter to access some array. If
Is it good practice to have a class constructor that uses default parameters, or
I have a windows form application that uses a Shared class to house all
I have a third party library that internally constructs and uses the SqlConnection class.
I have a class that looks like this public class SomeClass { public SomeChildClass[]
I have a class that uses the __set magic method. One of the properties
I discovered yesterday that I can't have a Class that uses a field named
So i have this class that includes some custom functions named class.php I have
I have class method that returns a list of employees that I can iterate
I have a class that I want to use to store properties for another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.