1

My MVC4 app uses code-first Entity Framework 5.0. I want to access my SQL Server data from a timer thread. Is there any reason why I can't instantiate, use, and dispose an instance of the same derived DbContext class that I also use on the main ASP.NET worker thread? (Yes, I use the same using() pattern to instantiate, use, and dispose the object on the main thread.)

A little problem context: My website has a WebsiteEnabled field in a table in the database. Currently, I incur a database fetch for each GET request to read that value. I want to change the code to read the value once every 15 seconds on a background thread, and store the value in a static variable that the GET request can read. I know that you run into problems if you try to instantiate multiple instances of the same DbContext on the same thread; I'm not sure if the same restrictions apply to instances of the same DbContext on different threads.

3
  • 1
    We use a background thread as well to check for emails and do cleanups every so often. As long as you create a new context (and dispose of it) on the background thread and not try and use the one from your main application thread, you will be fine. The DbContext is not thread safe, meaning you cannot share it across multiple threads. This does not mean you cannot have multiple threads each with their own copy of the db context. Only caution is beware of concurrency issues (trying to update a row at the same time). Commented Nov 22, 2013 at 5:58
  • @Tommy: Thanks. Please write this as an answer rather than a comment so I can accept it as The Answer. Commented Nov 28, 2013 at 17:36
  • No problem Bob, posted as an answer. Like I said, as long as you keep your DbContext localized to a single thread, you shouldn't have any issues. Commented Nov 28, 2013 at 22:41

2 Answers 2

1

We use a background thread as well to check for emails and do cleanups every so often in one of our larger MVC applications. As long as you create a new context (and dispose of it) on the background thread and not try and use the one from your main application thread, you will be fine. The DbContext is not thread safe, meaning you cannot share it across multiple threads safely. This does not mean you cannot have multiple threads each with their own copy of the db context. The only caution is beware of concurrency issues (trying to update a row at the same time).

Sign up to request clarification or add additional context in comments.

Comments

1

Statics and EF are recipe for a mess. Under asp.net, 1 app pool, many threads. Store statics if you must but not the context. So always make sure each thread gets its own context.

but given your problem, there is a simple out of the box solution I would use In thr controller that should have cached values. On the GET method You can cache per ID, for a specific period of time... Worth checking out. Let IIS, ASP.NET do work for you. :-)

 [OutputCache(Duration = int.MaxValue, VaryByParam = "id", Location = OutputCacheLocation.ServerAndClient)]
    public ActionResult Get(string id) {
        // the value that can be cached is collected with a NEW CONTEXT !

2 Comments

I don't understand this answer. I don't want to cache the page output; I want to cache a database read operation. Can you please post a more complete example?
dbcontext is not thread safe. the App pools use multiple threads. This example uses IIS to cache the result of get info for ID xyz. This will cache the result of your read. Caching gets is a very common requirement. Reuse existing capability is my suggestion. Research the OutputCache attribute.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.