So this is probably a stupid question, but I am still not exactly sure how the entity frameworks objects work. I am using EF4 in an MVC3 app, and have two controllers that need to use the same LINQ query against it. Is it best to use a static method that takes the db entity by ref, or should the method use a “using” block for its own entity (as seen in this question)?
I would think the using block would add additional overhead, but I didn’t find any examples of the other method. Is there a proper way to make “library” methods for EF access?
In an MVC application the
ObjectContextshould be scoped to the request. Most DI containers can do this automatically. So you would prefer not using ausingblock within a method. Instead inject the context via the constructor or pass it as a method argument.