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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:22:14+00:00 2026-05-13T12:22:14+00:00

What is the best way of working with linq data context? And how does

  • 0

What is the best way of working with linq data context? And how does link work with sql connection (when does it open connection and when does it close connection)? Should I aloways use using statement when i work with data context?

Sorry for list of questions. Thanks in advance.

  • 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-13T12:22:14+00:00Added an answer on May 13, 2026 at 12:22 pm

    There’s a lot to cover here – I’ll give you my potted view – and then give you some links etc to help you further.

    There are many other similar questions around this site as well, though – so do a search and you’ll find more.

    Lifetime

    • You don’t need to use a using statement with a Linq-To-Sql DataContext, but you do have to dispose it when you’re done. Therefore the using block is the best way because of it’s try … finally {} semantics. So, in my opinion you should use a using block.

    • You should not keep linq to sql data contexts open for long periods of time, because of the change tracking that occurs in every object that the context opens – it damages performances and can cause unexpected behaviour.

    • The Sql Connection, as far as I’m aware, is opened when the context is constructed, and closed when it is disposed.

    Therefore, you should open a linq to sql context when you want to select, insert, update or delete data, and then close it once you’re done. Inserting 100 records via the same DC is fine, for example; as is updating or deleting. However I probably wouldn’t select a few thousand records of one type, update a few thousand of another, then delete some more of another all in one context – primarily for performance reasons.

    Related Data

    If you have related data – i.e. Table A -> Table B, then know that the default for the DataContext (DC) is to lazy-load a related row in B when accessed from the child property on A. Therefore, if you intend to pass back one of your linq to sql-generated classes outside of the code that creates and disposes the DC that is used to select, you can get ObjectDisposedExceptions getting thrown.

    To avoid this – you can use the DataLoadOptions.LoadWith<> and DataLoadOptions.AssociateWith<> to mitigate this also – which forces a pre-fetch of related rows when one row is selected. This can increase the amount of data that your Linq To Sql queries read from the database, though, so you would not typically always apply prefetches of all related records for all queries – only on those that you know will require them.

    Sorry, I’m not giving code examples or anything else – there’s a lot to cover in one answer – but here’s some links:

    Rick Strahl talking about Linq To Sql DC lifetimes

    Dinesh of Microsoft talking about the same

    Scott Guthrie – 9 Part Linq to SQL Series – this is a linq to a search on his blog – scroll down, and page through, and you’ll find all of the different parts, starting with part 1 back in May 2007.

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

Sidebar

Related Questions

Possible Duplicate: Best way to prevent SQL injection? I'm working in one PHP project
my question is the following, which is the best way of working XML (kml)
I'm having trouble working out the best way to repaint a JxTreeTable when the
What's the best way to write an application in order to get it working
I'm working on a little MVC framework and I'm wondering what the best way
Working with the OpenSSL library in c, what is the best way to load
I have avoided working with fetchxml as I have been unsure the best way
What is the best way of working with calculated fields of Propel objects? Say
What is the best way of working with images, for a playing cards game
I'm trying to find the best way of working out whether the machine my

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.