Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework and calling context.dispose()

When should one call DbContext.dispose() with entity framework?

  1. Is this imaginary method bad?

    public static string GetName(string userId) {     var context = new DomainDbContext();     var userName = context.UserNameItems.FirstOrDefault(x => x.UserId == userId);     context.Dispose();     return userName; } 
  2. Is this better?

    public static string GetName(string userId) {     string userName;     using(var context = new DomainDbContext()) {         userName = context.UserNameItems.FirstOrDefault(x => x.UserId == userId);         context.Dispose();     }     return userName; } 
  3. Is this even better, that is, should one NOT call context.Dispose() when using using()?

    public static string GetName(string userId) {     string userName;     using(var context = new DomainDbContext()) {         userName = context.UserNameItems.FirstOrDefault(x => x.UserId == userId);     }     return userName; } 
like image 848
Sindre Avatar asked Mar 27 '13 18:03

Sindre


People also ask

Should I call Dispose on DbContext?

Don't dispose DbContext objects. Although the DbContext implements IDisposable , you shouldn't manually dispose it, nor should you wrap it in a using statement. DbContext manages its own lifetime; when your data access request is completed, DbContext will automatically close the database connection for you.

Does DbContext implement IDisposable?

Second, the DbContext class does indeed implement IDisposable out of the box.

Is DbContext scoped or Singleton?

First, DbContext is a lightweight object; it is designed to be used once per business transaction. Making your DbContext a Singleton and reusing it throughout the application can cause other problems, like concurrency and memory leak issues.


2 Answers

In fact this is two questions in one:

  1. When should I Dispose() of a context?
  2. What should be the lifespan of my context?

Answers:

  1. Never 1. using is an implicit Dispose() in a try-finally block. A separate Dispose statement can be missed when an exception occurs earlier. Also, in most common cases, not calling Dispose at all (either implicitly or explicitly) isn't harmful.

  2. See e.g. Entity Framework 4 - lifespan/scope of context in a winform application. In short: lifespan should be "short", static context is bad.


1 As some people commented, an exception to this rule is when a context is part of a component that implements IDisposable itself and shares its life cycle. In that case you'd call context.Dispose() in the Dispose method of the component.

like image 129
Gert Arnold Avatar answered Sep 20 '22 15:09

Gert Arnold


I followed some good tutorials to use EF and they don't dispose the context.

I was a bit curious about that and I noticed that even the well respected Microsoft VIP don't dispose the context. I found that you don't have to dispose the dbContext in normal situation.

If you want more information, you can read this blog post that summarizes why.

like image 39
Daniel Avatar answered Sep 22 '22 15:09

Daniel