0

I have some model class:

Public Class MyViewModel

   Public Property MyID() As Integer

   Public ReadOnly Property FirstList As IEnumerable(Of SelectListItem)
      Get
        Using dbContext As New MyContext
            Dim itemQuery = (From t In dbContext.ItemSet Select t)
            Dim item As IEnumerable(Of Item) = itemQuery.ToList()
            Return item.Select(Function(o) New SelectListItem() With {.Text = o.ItemDesc, .Value = o.ID})
        End Using
      End Get
   End Property

   Public ReadOnly Property SecondList As IEnumerable(Of SelectListItem)
     Get
        Using dbContext As New MyContext
            Dim _Query = (From t In dbContext.FrameworkSet Select t)
            Dim _list As IEnumerable(Of Item2) = _Query.ToList()
            Return _list.Select(Function(o) New SelectListItem() With {.Text = o.Item2Desc, .Value = o.ID})
        End Using
     End Get
  End Property
End Class

Basically, I'm calling MyContext twice. This instantiates EF repeatedly, correct? So my thought is just have a class global

Dim dbContext as New MyContext

Aside from Code Analysis telling me I need to implement IDisposable (which according to this: http://blog.jongallant.com/2012/10/do-i-have-to-call-dispose-on-dbcontext.html#.U6WdzrGEeTw I needn't worry about?)

I'm confused - what's the accepted best practice?

4
  • 1
    Your code is fine, wrapping the context in a using statement is perfectly fine. WHen the using block is completed Dispose will be called on the context as desired. Commented Oct 8, 2014 at 16:18
  • Deciding how to handle the 'lifespan' issues of an EF context depends a lot on the processing environment. Typically you would make very different choices if you were dealing with a Web application versus a WinForms, console app or similar desktop execution. Your post does not indicate the execution environment. Commented Oct 8, 2014 at 16:59
  • Sorry, David - Web Application. Commented Oct 8, 2014 at 17:38
  • 1
    NEVER EVER EVER EVER EVER EVER EVER EVER EVER EVER EVER... EVER. make an EF DbContext global/static in a web application. Static is global to all threads, and thus all users of your application, and DbContexts are neither thread safe, nor are they multi-user safe.. consider that you might add data to your DbContext in one user, then another user is also adding data to that context and calls SaveChanges when you aren't done yet... Commented Oct 8, 2014 at 22:06

2 Answers 2

1

In addition to Phil Soady's comments (which, briefly, are not to store the context in a global variable and instead prefer short lived disposed contexts) I'd like to point out that much of the context initialization is not done per construction of the object but rather once for the lifetime of the application. This is mainly the process of building its internal model, which it does and then caches.

Check out more detail here: http://blog.oneunicorn.com/2011/04/15/code-first-inside-dbcontext-initialization/

Sign up to request clarification or add additional context in comments.

Comments

0

Using block is ideal for EF. Global variable for context is a recipe for nightmares. The Context is not threadsafe and is intended for short use. Keeping the context for several operations in a logical flow is common. Since the context content can be reused. Change detection , unit of work commit control are all part of EF. But dont try a keep the context for an extended period. You may have more performance problems with the context that way as the context may grow. You also have the multi user and concurrency issues to consider. Even a simple standalone APP on PC i would Create and dispose (using) the content on each "button" click in an app.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.