I am using the line of code below and getting the error:
Type used in a using statement must be implicitly convertible to System.IDisposable
How do I eradicate this error? This is the line of code:
using (var db = new HealthTrackerContext())
I am using the line of code below and getting the error:
Type used in a using statement must be implicitly convertible to System.IDisposable
How do I eradicate this error? This is the line of code:
using (var db = new HealthTrackerContext())
If your HealthTrackerContext
does not implement IDisposable
, you will get this error. I suspect your class does not; so to remedy this situation, you either need to implement IDisposable
on your object or remove the using
block.
There's nothing wrong with not using a using
block, btw =D
HealthTrackerContext should implement the IDisposable interface; Once you implement that interface, the error would disappear.
Look at this link http://msdn.microsoft.com/en-us/library/system.idisposable.aspx
It means HealthTrackerContext
doesn't implement IDisposable
.
A copy-easy and thread safe pattern:
public partial class HealthTrackerContext: IDisposable {
protected virtual void Dispose(bool disposing) {
lock(thisLock)
if(!disposed) {
if(disposing) {
// release your resource
// and set the referencing variables with null
this.disposed=true;
}
}
}
public void Dispose() {
this.Dispose(true);
GC.SuppressFinalize(this);
}
~HealthTrackerContext() {
this.Dispose(false);
}
object thisLock=new object();
bool disposed;
}
you must implement IDisposable Interface for this object like that :
public class Disposable : IDisposable
{
private bool isDisposed;
~Disposable()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (!isDisposed && disposing)
{
DisposeCore();
}
isDisposed = true;
}
}