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

  • SEARCH
  • Home
  • 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 7403823
In Process

The Archive Base Latest Questions

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

So I need some advice and insight here. Thanks in advance for your thoughts.

  • 0

So I need some advice and insight here. Thanks in advance for your thoughts.

I have developed static functions that return a single record from a LINQ entity. Like so:

FooRecord GetRecord(Guid id)
{
   using(var dc = new FooDataContext())
       return dc.FooRecords.Where(a => a.Id == id).First();
}

This throws an exception because the DataContext is already disposed, which creates problems with deferred execution. This works:

FooRecord GetRecord(Guid id)
{
       var dc = new FooDataContext();
       return dc.FooRecords.Where(a => a.Id == id).First();
}

I am worried. How quickly will the DataContext be disposed? Obviously if I grab the record immediately this won’t cause an issue. However, say I need to grab a record through association:

var record = Data.FooRecord.GetRecord(id);
//Do a bunch of stuff...
//Now we grab the related record from another entity
var barRecord = record.BarRecord

Is there a risk the DataContext be gone by this point? Any advice?

  • 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-29T05:09:58+00:00Added an answer on May 29, 2026 at 5:09 am

    You basically do not need to Dispose() your DataContext for the reasons discussed here:

    When should I dispose of a data context

    http://csharpindepth.com/ViewNote.aspx?NoteID=89

    The main reason for implementing IDisposable on a type is to dispose of any unmanaged resources. The only unmanaged resource allocated by the DataContext is the underlying database connection, but the DataContext already takes care of opening and closing the connection as needed.

    The main thing you want to avoid is returning an IEnumerable collection and then never enumerating it, as this will cause the connection to remain open indefinitely. However, since you are only returning a single object, you shouldn’t have to worry about this.

    Also note that if access any relationship property on the returned object it may cause the connection to be momentarily reopened so that the property can be lazy loaded. You can avoid this by using DataLoadOptions.LoadWith() with your DataContext to eager-load any properties you intend to access. See http://msdn.microsoft.com/en-us/library/system.data.linq.dataloadoptions.aspx

    As to the last part of the question, if the returned entities contain properties that can be lazy loaded, then they will contain internal references to back the DataContext that will keep it in memory. Once you have no more references to these entities, then the DataContext will of course be garbage-collected just like any other object.

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

Sidebar

Related Questions

I need some advice here, I hope somebody can help me. I have the
I need some advice with the Java Print API. I have a program that
I need some advice here. I need to make a webservice, that can make
I need some advice. I’m building an app that has a sequence of 4
I need some advice of how to setup my tables I currently have a
I need some advice for a design which I have to implement in application.
I need some advice on a DB I'm working in. I have the DB,
Need some advice, I'm after a decent process/task manager for Ubuntu. Basically I have
Need some advice again please. This is regarding a single developer shop. I have
I need some advice how to implement this situation in my application. I have

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.