Should Entity Framework Context be Put into Using Statement?

前端 未结 9 1668
不思量自难忘°
不思量自难忘° 2020-11-29 23:52

The Entity Framework context object implements a Dispose() method which \"Releases the resources used by the object context\". What does it do really? Could it be a bad thin

相关标签:
9条回答
  • 2020-11-29 23:57

    EF5 and before 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
    

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

    0 讨论(0)
  • 2020-11-29 23:58

    Always, if you instantiate a class that implements IDisposable, then you are responsible for calling Dispose on it. In all but one case, this means a using block.

    0 讨论(0)
  • 2020-11-30 00:07

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

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

    2. If you use the context for a longer period - that is the life time is not bound by the execution time of a method - you cannot use the using statement and you have to call Dispose() yourself and take care that you always call it.

    What does Dispose()do for an object context?

    I did not look at the code, but at least I exspect it to close the database connection with its underlying sockets or whatever resources the transport mechanism used.

    0 讨论(0)
  • 2020-11-30 00:09

    If Dispose closes connection to DB it is a bad idea to call it. For example in ADO.NET connections are in connection pool and never be closed before timeout or application pool stop.

    0 讨论(0)
  • 2020-11-30 00:11

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

    So in short, while the using statement isn't required, it's best practice if you know you're done using the ObjectContext since the resource is freed up immediately instead of waiting for garbage collection.

    0 讨论(0)
  • 2020-11-30 00:12

    When you dispose, the ObjectContext disposes other owned objects.

    Including things like the EntityConnection which wraps the actual database connection, i.e. generally a SqlConnection.

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

    0 讨论(0)
提交回复
热议问题