Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

IDisposable implementation for entity framework DbContext

$
0
0

I have a private field that holds instance of dbContext in my class that i am initialize in the class constructor.  
I would like to implement IDisposable interface on that class so it will execute dbContext.dispose when disposing.

I am not sure if i need to do something in ~BaseDal() and in Dispose() function.

In other words, if i am using  dbContext.Dispose() in my Dispose(bool disposing) function. is means i am releasing only manage code or also unmanage code?

public class ContextHolder:IDisposable
    {
        private MyDbContext _dbContext;
        ContextHolder()
{ _dbContext = new JungleDbContext(); } public DbRawSqlQuery<TElement> ExecuteSP<TElement>(string spName, params object[] parameters) { return _dbContext.Database.SqlQuery<TElement>(spName, parameters); } #region IDisposable Support private bool disposedValue = false; // To detect redundant calls protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { _dbContext.Dispose(); } // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below. // TODO: set large fields to null. disposedValue = true; } } // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources. // ~BaseDal() { // // Do not change this code. Put cleanup code in Dispose(bool disposing) above. // Dispose(false); // } // This code added to correctly implement the disposable pattern. public void Dispose() { // Do not change this code. Put cleanup code in Dispose(bool disposing) above. Dispose(true); // TODO: uncomment the following line if the finalizer is overridden above. // GC.SuppressFinalize(this); } #endregion }

Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>