Should the Framework Entity context be used in using Statement?

The Entity Framework object object implements the Dispose () method, which "Releases the resources used by the object context." What is he really doing? Maybe it's bad to always use it in using {} statements? I saw that it is used both with and without using.

I specifically use the EF context from the WCF service method, create the context, do some linq and return the response.

EDIT: It seems like I'm not the only one who wonders about this. Another question is what really happens inside the Dispose () method. Some say it closes connections, and some articles say no. What a deal?

+60
c # entity-framework
May 05 '09 at 10:49 a.m.
source share
9 answers

If you create a context, you must dispose of it later. If you must use the using statement, it depends on the lifetime of the context.

  • If you create a context in a method and use it only in this method, you really should use the using statement because it gives you exception handling without any additional code.

  • If you use the context for a longer period β€” this lifetime is not related to the execution time of the method β€” you cannot use the using statement, and you need to call Dispose() yourself and make sure you always call it.

What does Dispose() for an object context?

I did not look at the code, but at least I am considering it to close the database connection with my base sockets or any resources that used the transport mechanism.

+31
May 05 '09 at 11:06 a.m.
source share

Since you do not know when the garbage collector places an element, it is always useful to wrap objects that implement IDisposable in the block in use, if you know when to finish it.

+4
May 05 '09 at 11:06
source share

Per Progamming Entity Framework : "You can either explicitly place the ObjectContext or wait for the garbage collector to do the job."

In short, while the using statement is not required, it is best if you know that you have finished using the ObjectContext, as the resource is immediately freed and not waiting for garbage collection.

+4
Nov 11 '10 at 19:43
source share

EF5 and up to version

  using { ... // connecction open here. ... context.Blogs.Add(blog); context.SaveChanges(); // query etc now opens and immediately closes ... context.Blogs.Add(blog); context.SaveChanges(); // query etc now opens and immediately closes } 

EF6 and after version

  using { // connecction open here. ... context.Blogs.Add(blog); context.SaveChanges(); // The underlying store connection remains open for the next operation ... context.Blogs.Add(blog); context.SaveChanges(); // The underlying store connection is still open } // The context is disposed – so now the underlying store connection is closed 

Link: http://msdn.microsoft.com/en-us/data/dn456849#open5

+3
Apr 22 '14 at 18:50
source share

Always, if you create an instance of a class that implements IDisposable, then you are responsible for calling Dispose on it. In all cases except one, this means using a block.

+1
May 05 '09 at 10:54 a.m.
source share

When placing an ObjectContext, other objects belonging to it are located.

Including things like EntityConnection that wrap the actual database connection, i.e. usually SqlConnection.

So, if 'SqlConnection is open, it will be closed when you place the ObjectContext.

+1
May 05 '09 at 17:16
source share

I really checked this thing for both ADO.net and EF v.6 and looked at the joins in the SQL table

 select * from sys.dm_exec_connections 

Test methods should look like this:

1) ADO.net using

  using(var Connection = new SqlConnection(conString)) { using (var command = new SqlCommand(queryString, Connection)) { Connection.Open(); command.ExecuteNonQueryReader(); throw new Exception() // Connections were closed after unit-test had been //finished. Expected behaviour } } 

2) ADO.net using

 var Connection = new SqlConnection(conString); using (var command = new SqlCommand(queryString, Connection)) { Connection.Open(); command.ExecuteNonQueryReader(); throw new Exception() // Connections were NOT closed after unit-test had been finished finished. I closed them manually via SQL. Expected behaviour } 

1) EF using.

  using (var ctx = new TestDBContext()) { ctx.Items.Add(item); ctx.SaveChanges(); throw new Exception() // Connections were closed, as expected. } 

2) EF without using

  var ctx = new TestDBContext(); ctx.Items.Add(item); ctx.SaveChanges(); throw new Exception() // Connections WERE successfully closed, as NOT expected. 

I do not know why this is so, but EF automatically closes the connections. Also, all repository and UnitOfWork templates that use EF do not use usage. This is very strange for me, because DBContext is a one-time type, but it is a fact.

Perhaps they did something new at Microsoft for processing?

+1
Nov 07 '13 at 21:20
source share

I noticed (albeit in one application) that explicit deletion caused thread interruptions in mscorlib that were captured before the application code, but at least in my case, which led to a noticeable result. You have not done any significant research on this, but it is probably worth considering if you are doing this. Just look at your DEBUG output to see if you get the same result.

0
Mar 27 '13 at 12:36
source share

If Dispose closes the DB connection, it is a bad idea to name it. For example, in ADO.NET, connections are in the connection pool and never close before the timeout or application pool expires.

0
Jun 10 '13 at 9:39
source share



All Articles