Entity Framework and Multi threading

前端 未结 6 584
走了就别回头了
走了就别回头了 2020-12-02 12:26

We are having some trouble designing our multi-threaded Entity Framework driven application and would like some guidance. We are creating entities on different threads, the

6条回答
  •  时光取名叫无心
    2020-12-02 13:06

    You do -not- want a long lived context. Ideally they should be for the life of a request/data operation.

    When dealing with similar problems, I wound up implementing a repository that cached PK Entities for a given type, and allowed a 'LoadFromDetached' which would Find the entity in the database, and 'copy' all the scalar properties except PK over to the newly attached entity.

    The performance will take a bit of a hit, but it provides a bullet proof way of making sure navigation properties don't get mangled by 'forgetting' about them.

提交回复
热议问题