How to use injected DbContext in parallel methods in asp.net core and ef7?

老子叫甜甜 提交于 2019-12-06 01:08:26
YuriyP

I also faced with this problem and for now I use my temporary solution EF 7 (Core). Create DBContext like AddTransient

If someone can provide more elegant solution I will be appreciated. Sometimes I really need to return instant result to user and in such cases await/async does not suit

Simple answer: you can't. EF is not made for parallel sql execution (and never will be, I guess).

Less-simple answer. Probably this is not the right way to approach your problem (@Tseng made a point in his comments), but if you need to do a logging stuff like that you can:

  • make the two logging action sequential and share the same EF instance
  • use plain old SQL with a SqlConnection object (which works perfectly with parallel processing) - and it's faster
  • use a logging framework like NLog which supports log-and-forget approach
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!