Is it safe to use one Entity Framework Context per thread? … yes? how?

前端 未结 2 1817
情深已故
情深已故 2021-01-15 13:25

Probably these are two questions in one, I am using one EF context per request, but I want to use one per thread, because I am going to make some complex task in another thr

2条回答
  •  情歌与酒
    2021-01-15 14:03

    I would limit the scope of the EF context to a particular unit of work on a request level.

    If you have units of work that are unrelated to requests by all means you can spawn a new thread for that and use a new EF context, just dispose of it once that unit of work has completed. Be aware though that EF contexts are not thread safe, i.e. you cannot use the same context in more than one thread.

提交回复
热议问题