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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:08:44+00:00 2026-05-23T05:08:44+00:00

I have some questions regarding the entity framework context instance management. My code base

  • 0

I have some questions regarding the entity framework context instance management.

My code base is composed of many independent components/agents all running in one process that can only do work by reading messages from their corresponding queues at their convenience(whenever the component is ready, it will pick up the next message from the queue so no concurrency issue at the component level)

Each of the components need to interact with the database independent from other components. I am wondering what is the better way to setup the context instance/s for each component. Here are some of the options

1> Have one instance of the the context used by all components. –> I think this is the worst as it creates many concurrency issues?

2> Give every component an independent instance of the context. –> This looks fine but

  • is it ok to have many context independent instances in one process while all components are running concurrently in this process?

  • should I create a new instance of context for every new message that the component will process or keep one context instance for the life of the component? I think the last one makes more sense but I am more used to use context in a using{} bracket and I am not sure if keeping one context live for the life of each component has any complications in the way I am using it?

  • can I rely on optimistic concrruency so that two different independent components won’t put same record in the database given all contexts are in one process?

BTW, I am using entity framework 4.1.

  • 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-23T05:08:45+00:00Added an answer on May 23, 2026 at 5:08 am

    Definitely use one context per component / agent – if component is multithreaded use one context per thread. If each message processing is executed as separate “logical transaction” then use one context per message processing.

    Why:

    • Context internally uses two very important design patterns – Identity map and Unit of work. This answer describes the behaviour enforced by these patterns.
    • Context and anything else in EF is not thread safe.

    Optimistic concurrency doesn’t mean that different contexts will not put the same record in the database. Optimistic concurrency means that update statements compare current state in the database with last known state by the context (there is a delay between loading record and saving new values and another context can change the record). If record changed you will get an exception and you must handle it somehow.

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

Sidebar

Related Questions

I have some questions regarding data base design for simple CMS. Every Page in
I have some questions regarding the following code: 1 class Test(object): 2 def __init__(self):
I have some questions regarding COM memory management: I have a COM method: STDMETHODIMP
I have some questions regarding the following css that I found: html, body {
I've read some documentation on how Adaboost works but have some questions regarding it.
There have been some similar questions asked regarding Grid views, but none have been
I have couple questions regarding some C++ rules. Why am I able to call
I have some questions with relationship of ef code first. My code: public class
I have some questions regarding my MVC learning curve Goal: I want to get
I'm new to funcctional programming and have some questions regarding coding style and debugging.

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.