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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:19:18+00:00 2026-05-30T06:19:18+00:00

What is the best practice for working with entity framework in a multi threaded

  • 0

What is the best practice for working with entity framework in a multi threaded server?
I’m using entity framework ObjectContext to manage all my database actions, now I know this context isn’t thread safe, so for now when I need to use it to perform some db actions I surround it with lock statement to be safe. Is this how I should do it??

  • 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-30T06:19:20+00:00Added an answer on May 30, 2026 at 6:19 am

    Some quick advices for Entity Framework in a multi-threaded environment:

    • Don’t use a unique context with locks (no singleton pattern)
    • Provide stateless services (you need to instantiate and dispose one context per request)
    • Shorten the context lifetime as much as possible
    • Do implement a concurrency-control system. Optimistic concurrency can be easily implemented with Entity Framework (how-to). This will ensure you don’t overwrite changes in the DB when you use an entity that is not up-to-date

    I’m a bit confused, I thought that using one context is good because
    it does some catching I believe, so when I’m dealing with the same
    entity in consecutive requests it be much faster to use the same
    context then creating a new context every time. So why does it good to
    use it like this if It slower and still not thread safe?

    You could use only one context, but it’s strongly discouraged unless you really know what you are doing.

    I see two main problems that often happen with such an approach:

    1. you’ll use a lot of memory as your context will never be disposed and all manipulated entities will be cached in memory (each entity that appears in the result of a query is cached).

    2. you’ll face lots of concurrency issues if you modify you data from another program/context. For example, if you modify something directly in your database and the associated entity was already cached in your unique context object, then your context won’t ever know about the modification that was made directly in the database. You’ll work with a cached entity that is not up-to-date, and trust me, it’ll lead to hard-to-find-and-fix issues.

    Also don’t worry about performances of using multiple contexts: the overhead of creating/disposing a new context per request is almost insignificant in 90% of use cases. Remember that creating a new context does not necessarily create a new connection to the database (as the database generally uses a connection pool).

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

Sidebar

Related Questions

What is the best practice for a team working on a same Database? Should
When working with an MVC framework and querying the database from the controller using
I am working with VS 2010, Entity framework, SQl-Server 2005, ASP.Net web forms .
Im searching for the best practice (or any working solution) for the following scenario:
What is best practice for C++ Public API? I am working on a C++
The major shortcomings with Entity-Attribute-Value database designs in SQL all seem to be related
This is more or less a best-practice question: I'm working on a site that
giving a database table with huge data in it, what is the best practice
I'm using the Entity Framework as my ORM for an ASP.Net (webforms) application. Suppose
I am using Entity Framework 4 in my project. One basic requirement is to

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.