我有很多存儲庫類使用DbContext
變量。初始化這個DbContext
變量的最佳做法是什麼?爲基本類C中的存儲庫初始化dbcontext#
我試圖與繼承IDisposable
爲此在基類,但我的應用程序中進行操作的很多在一次和我得到DataReader的是忙中出錯。但是當我使用這個函數:using(var _entities = MyDbContext.Create())
都好嗎?什麼是問題,解決問題的最好方法是什麼?
現在我的基類是這樣的:
public abstract class BaseRepository : IDisposable
{
protected readonly MyDbContext_entities;
public BaseRepository()
{
_entities = MyDbContext.Create();
}
#region Implement IDisposable
private bool disposed = false;
protected virtual void Dispose(bool disposing)
{
if (!this.disposed)
{
if (disposing)
{
_entities.Dispose();
}
}
this.disposed = true;
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
#endregion
}
,並用我的回購之一獲得與構造此值:
public MyRepo():base(){};
功能MyDbContext.Create()
:
public static MyDbContext Create()
{
return new MyDbContext();
}
我知道泛型類型,但有很多方法,我希望有一種方法來解決這個問題,也許基類 – NoNameR