# Injecting EF Context per-thread get a new instance per job?

**URL:** <https://discuss.hangfire.io/t/injecting-ef-context-per-thread-get-a-new-instance-per-job/703>\
**Category:** question\
**Created:** [February 23, 2015, 1:14pm UTC](https://discuss.hangfire.io/t/injecting-ef-context-per-thread-get-a-new-instance-per-job/703 "2015-02-23T13:14:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben\_Ford](https://avatars.discourse-cdn.com/v4/letter/b/9de053/32.png) [@Ben\_Ford](https://discuss.hangfire.io/u/Ben_Ford)\
**Post date:** [February 23, 2015, 1:14pm UTC](https://discuss.hangfire.io/t/injecting-ef-context-per-thread-get-a-new-instance-per-job/703/1 "2015-02-23T13:14:52Z")

</div>

I’m injecting my EF context using Ninject using an ‘InThreadScope’. This works fabulously except for if an exception occurs on the context. This makes the context stale and causes ALL subsequent jobs on the same worker # to also fail (As they run in the same thread).

Is there a recommended way to handle such a situation? Or is it best to just dispose my context at the end of each job to force Ninject to spawn a new one?

Alternatively if there is a way to force each worker to run in a fresh thread for each job. This would resolve this (and any other thread specific issues) in the future.

---

<div class="post-metadata">

**Author:** ![nirinchev](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/nirinchev/32/122_2.png) [@nirinchev](https://discuss.hangfire.io/u/nirinchev)\
**Post date:** [February 23, 2015, 2:50pm UTC](https://discuss.hangfire.io/t/injecting-ef-context-per-thread-get-a-new-instance-per-job/703/2 "2015-02-23T14:50:04Z")

</div>

I would spawn a new context for each job since reusing the same context over a long period of time can cause all kinds of issues.

---

<div class="post-metadata">

**Author:** ![youngcm2](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/youngcm2/32/145_2.png) [@youngcm2](https://discuss.hangfire.io/u/youngcm2)\
**Post date:** [February 23, 2015, 5:27pm UTC](https://discuss.hangfire.io/t/injecting-ef-context-per-thread-get-a-new-instance-per-job/703/3 "2015-02-23T17:27:58Z")

</div>

We had to create a UnitOfWorkScope that disposed after the job completed.

Here is a repository with a sample.

> **[youngcm2/Hangfire.JobScope](https://github.com/youngcm2/Hangfire.JobScope)**
>
> Contribute to youngcm2/Hangfire.JobScope development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![youngcm2](https://dub1.discourse-cdn.com/flex017/user_avatar/discuss.hangfire.io/youngcm2/32/145_2.png) [@youngcm2](https://discuss.hangfire.io/u/youngcm2)\
**Post date:** [February 23, 2015, 5:29pm UTC](https://discuss.hangfire.io/t/injecting-ef-context-per-thread-get-a-new-instance-per-job/703/4 "2015-02-23T17:29:06Z")

</div>

We also use the UnitOfWorkScope through a controller request on our website. It is similar to InRequestScope for Ninject.
