简体   繁体   中英

Do I need to Create A Deconstructor in C# When The Class Uses Entity Framework?

May be a dumb question, but is it prudent to create a deconstructor class on a called class that is using Entity Framework under asp.net framework 4.x? Does it provide faster release of memory elements in the class concerning entity?

I know that it's recommended to create a dispose method when calling entity directly in the mvc controller class.

Here's a sample of what I mean...

    public class DepartmentServices
    {
        private MyDB _db = new MyDB();

        //Deconstructor class
        ~DepartmentServices()
        {
            _db.Dispose();
        }

That's the finalizer method. Your class should implement IDisposable instead. Take a look at the IDisposable pattern: https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose

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