Can Entity Framework context be reused all the time in code?

后端 未结 1 747
暖寄归人
暖寄归人 2021-01-28 02:48

In this question I was having problem with saving objects that had foreign keys because Objects were build from multiple Objects connected to each othe

相关标签:
1条回答
  • 2021-01-28 03:33

    But what if I introduce multithreading, try to get multiple values from all over the place for different objects (load object to GUI, to ListView etc) using the same Context? Won't it blow back on me hurting me severely ?

    Yes, yes it will. A context is basically a thin layer on top of a database connection - which is not thread safe, so you cannot reuse the same context across threads. What you are looking for is a unit of work within which you use the same context, but once that unit of work is completed you dispose the context. Since you use your own repository implementation you will have to build the unit of work on top of those repositories.

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