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 7510387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:12:57+00:00 2026-05-29T23:12:57+00:00

I am creating a LINQ to SQL DataContext by passing it an already open

  • 0

I am creating a LINQ to SQL DataContext by passing it an already open connection. This works, except I’m now leaking connections everywhere because although I dispose the DataContext when I’m finished, the connection never gets closed. I understand this is by design.

What I would like to do is ensure that my connection gets closed once my DataContext is disposed.

I have tried to override Dispose on the DataContext, like this:

protected override void Dispose(bool disposing)
{
    this.Connection.Close();
}

However, this does not work… I get an ObjectDisposedException. Setting a breakpoint shows me that everything is already disposed at this point!

The only workaround I have found, is to hide the Dispose method on DataContext. Like this:

    public new void Dispose()
    {
        this.Connection.Close();

        base.Dispose();
    }

However, this has a bit of a bad code smell to me. What is the recommended way to proceed here?

Complete code sample (DataClasses1DataContext is an empty DataContext):

class Program
{
    static void Main(string[] args)
    {
        string connectionString = "server=localhost;initial catalog=master;Integrated Security=SSPI;";

        for (int i = 0; i < 100; i++)
        {
            var connection = new SqlConnection(connectionString);
            connection.Open();

            var ctx = new DataClasses1DataContext(connection);
            ctx.ExecuteCommand("declare @i int");

            ctx.Dispose();
        }

        Console.ReadKey();
    }
}

public partial class DataClasses1DataContext
{
    protected override void Dispose(bool disposing)
    {
        // This will throw an ObjectDisposedException
        // this.Connection.Close();
    }

    public new void Dispose()
    {
        // This will work
        // this.Connection.Close();

        //base.Dispose();
    }
}
  • 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-29T23:12:57+00:00Added an answer on May 29, 2026 at 11:12 pm

    The problem is Dispose() sets disposed before calling Dispose(bool). This causes the exception when you call Connection. There does not appear to be a way around this.

    However I have to ask, why is this necessary? Dispose(bool) calls Dispose on the Provider which contains the Connection and should handle closing it automatically.

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

Sidebar

Related Questions

Are there more extension points available in linq-to-sql than creating your own partial DataContext
I am creating a linq-to-sql model with EF, and have a class that works
I am creating serialized XML for a LINQ to SQL project using the DataContractSerializer
I am creating a website and using Linq to SQl as a data access
I'm using LINQ to Entities (not LINQ to SQL) and I'm having trouble creating
I need help re-factoring this legacy LINQ-SQL code which is generating around 100 update
I use Linq to Sql (although this is equally applicable in Entity Framework) for
I'm considering using LINQ-to-SQL for creating the DAL in my app. I normally create
Linq to SQL and Linq to Entities depend on creating dynamic SQL to do
In Linq To Sql, when updating one of my entities, Faculty, I am creating

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.