Entity Framework Core thread safe?

后端 未结 1 1257
旧巷少年郎
旧巷少年郎 2020-12-06 13:51

I follow Generic Repository Pattern in ASP.NET Core, but on IRepository, I use IQueryable instead of IEnumerable:

publ         


        
相关标签:
1条回答
  • 2020-12-06 14:34

    Entity Framework DbContext is not thread safe. You can execute only one query at a time otherwise you will get an exception like you did above.

    I suppose that you use our repository multiple times during same request in parallel that's why you get the exception. If you don't create a transaction per request you can simply make repository transient. In this case new repository will be created fro each instance of your service and you will avoid concurrency issue.

    0 讨论(0)
提交回复
热议问题