Entity Framework lazy loading doesn't work from other thread

前端 未结 1 1584
孤街浪徒
孤街浪徒 2020-12-09 13:38

I just found out that lazy loading in Entity Framework only works from the thread that created the ObjectContext. To illustrate the problem, I did a simple test

相关标签:
1条回答
  • 2020-12-09 13:55

    Is this by design? Yes; any call to Load, implicit or explicit, will eventually go through the ObjectContext, and ObjectContext is documented to be not thread-safe.

    A possible workaround would be to detach the entity from the object context in the worker thread and attach it to an object context in the current thread.

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