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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T21:26:27+00:00 2026-06-06T21:26:27+00:00

when using Linq-to-Sql, Visual Studio creates a DataContext class for you. I’m wondering when

  • 0

when using Linq-to-Sql, Visual Studio creates a DataContext class for you. I’m wondering when to instantiate this context, in one of my projects I used one DataContext instanse for about 300 CRUD commands (whole project) and it worked with a little problem which I managed to solve later, and in my recent project I have created an instance of it per CRUD command, now here’s the question, what is the best way to use DataContext? one per command or one per project? or maybe one per transaction (that I thinks is a good idea too)?
And is there any difference in speed and performance it each way?

  • 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-06-06T21:26:28+00:00Added an answer on June 6, 2026 at 9:26 pm

    When you keep it open too short you waste performance because instantiating a DataContext is not exactly free. It is cheap but measurable. It shows up in profiles.

    When you keep it open too long you get problems too:

    1. Objects start to accumulate in it
    2. You can’t use it in multiple threads or multiple transactions at the same time anyway
    3. If one of your SubmitChanges fails for some logical reason, the context is dead because the same error will appear forever, no matter how often you retry

    Here are some rules what you should never do without deep understanding:

    1. Have one DataContext for multiple HTTP-requests/WCF-calls
    2. Have a DataContext for an unbounded amount of time (i.e. the duration a desktop app is nunning)
    3. Have a DataContext for an unbounded number of tracked entities
    4. Use it across threads
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working in Visual Studio 2010 using linq-to-sql's DataContext which has several maps to
I have create a LINQ-to-SQL project in Visual Studio 2010 using Dynamic Data. In
Right now i'm exploring LINQ-to-SQL using Visual Studio 2010 beta. I'm trying to understood
I am using Visual Studio 2010, and I have a LINQ-to-SQL DBML file that
I am using visual studio 2010, (desktop application) and using LINQ to SQL to
I have been using Linq to SQL for a while now on one of
I have created a Area class using Linq-to-SQL. Now I want to create a
I am new to LINQ. I am using Visual Studio 2008 and have created
I am using visual studio 2008 and SQl Server 2008. I recently formatted my
Visual Studio 2008 | Silverlight 3 | SQL Server 2005 | Domain Services Class

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.