简体   繁体   中英

Importance of disposing EF DbContext before the console app exits in C#

I am writing a simple console application that does only the following:
1. Query the database for some data.
2. Process this data.
3. Update the database.

I wrote the code, which consists only of the Main method, like the following:

class Program
{
    static void Main(string[] args)
    {
       try
       {
          var dbContext = new MyDatabaseContext();
          var dbRecord = dbContext.MyTable.First(r => r.Status == 1);

          // Do some work

          dbRecord.Status = 2;
          dbContext.SaveChanges();   
       }
       catch(Exception)
       {
         // left empty
       }    
    }
}

A colleague of mine told me that I must enclose the code within a "using" statement to close the connection between the application and the database server, like the following:

class Program
{
    static void Main(string[] args)
    {
       try
       {
           using(var dbContext = new MyDatabaseContext())
           {
              var dbRecord = dbContext.MyTable.First(r => r.Status == 1);

              // Do some work

              dbRecord.Status = 2;
              dbContext.SaveChanges();      
           } 
       }
       catch(Exception)
       {
         // left empty
       }  
    }
}

I know the importance of disposing an "IDisposable" object, before leaving a scope or when the object is no longer needed, to avoid memory leak and to release resources.

But my understanding is, in my case, the program already ends and I don't need to explicitly dispose the DbContext as no connection will exist between the application and the database server after the application ends.

So, I need to answer the following:

1.Is it important, in my case, to dispose the DbContext object before the program exits?
2. Will the connection be still open even after closing the program (Normally, with an exception, or closed by the user)?
3. What will happen if I don't use the "using" statement?

I will be thankful if you provide the answers with official references.

Please note that my concern is memory and resource leaks. Data loss is not my concern for now.

To answer to your 3 questions:

  • EF tutorial docs say: "...Also, in most common cases, not calling Dispose at all (either implicitly or explicitly) isn't harmful.". I don't trust most common cases. It's a best practice to do it, and I would. By doing it, you are implicitly sure that every resource your context is using is properly released because the maintainers of this framework say so.
  • No, no connection opened because the object holding the connection lives in your program's memory. After the OS chooses to kill the process, there is no more pointer to that connection object. If it's still in memory, it's unreachable so it's like it does not exist. I don't know if Windows implements some strange global connection pooling but, if so, it would be OS-specific and I would never rely on that.
  • The using statement defines a scope at the end of which an object will be disposed. So, it translates in implicitly calling the dispose method when the end of the using block is reached. It's cosmetic and helps you better define scopes in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM