简体   繁体   English

当类使用实体框架时,我需要在 C# 中创建一个析构函数吗?

[英]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?可能是一个愚蠢的问题,但是在 asp.net 框架 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.我知道在 mvc 控制器类中直接调用实体时,建议创建一个 dispose 方法。

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.你的类应该实现 IDisposable 。 Take a look at the IDisposable pattern: https://docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose看看 IDisposable 模式: https : //docs.microsoft.com/en-us/dotnet/standard/garbage-collection/implementing-dispose

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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