如何实现IDisposable在实体框架? [英] How to implement IDisposable in Entity Framework?

查看:154
本文介绍了如何实现IDisposable在实体框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个单独的EL层,代表实体层我的实体框架上下文,然后我移动到DAL,然后BL和我的用户inteface aspx.cs代码页。
我很困惑,例如如何在相同的使用IDisposable接口。
我做的到现在什么,supopose在我DAL我有我的实体上下文。

I am having my entity framework context in a separate EL Layer, which stands for Entity Layer and then I move to DAL, then BL and my user inteface aspx.cs code page. I am confused as such how to use IDisposable in the same. What I am doing till now, supopose in my DAL I have context of my entities.

namespace abc
{
    public class Action: IDisposable
    {
        Entities context = new Entities();
        // all the methods

        public void Dispose()
        {
            context.Dispose();
        }
    }
}



是不是正确的方式这样做?
我只是一个天真的程序员,所以帮我在学习相同的逻辑。

Is it the correct way of doing so? I am just a naive programmer so help me in learning the same logic.

推荐答案

我个人会改变它点点,如:
我虽然有实施的IDisposable 实体框架内

Personally I would change it a little bit, such as: Although I have very little experience with implementing the IDisposable within the Entity Framework.

namespace abc
{
    public class Action: IDisposable
    {
        private bool _disposed;

        Entities context= new Entities();
        // all the methods

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected virtual void Dispose(bool disposing)
        {
            if (!_disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                    // Dispose other managed resources.
                }
                //release unmanaged resources.
            }
            _disposed = true;
        }
    }
}

这篇关于如何实现IDisposable在实体框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆