ObjectContext is for the unit of work.
In essence, this means that for each “operation” (for example: every request to a web page) there must be a new instance of ObjectContext. As part of this operation, you must reuse the same ObjectContext.
This makes sense when you think about it, because transactions and change propagation are tied to an ObjectContext instance.
If you are not writing a web application and instead writing a WPF application or Windows forms, it becomes a little more complicated, since you do not have a narrow request area, downloading gives you, but you get an idea.
PS: In any of your examples, the lifetime of an ObjectContext will be either global or temporary. In both situations, it should not be inside the DataAccess class - it should be passed as a dependency
Orion edwards
source share