C#/EF and the Repository Pattern: Where to put the ObjectContext in a solution with multiple repositories?

前端 未结 2 1069
梦毁少年i
梦毁少年i 2020-12-09 07:07

I have multiple repositories in my application. Where should I put the ObjectContext? Right now, I have a reference like ObjectContext ctx; in every repositor

2条回答
  •  既然无缘
    2020-12-09 07:33

    What I Normally do is create a ObjectContext once and store it in Thread Local Storage (using NamedThreadDataSlot) and access the ObjectContext from there. This ensures that the ObjectContext is shared across the request, which will be handled by one thread.

提交回复
热议问题