I know the caching of DbContext is not good idea. But I would like to do it fine. What do you think about this way?
public class Context : DbContext
{
private Context()
{
}
private static WeakReference<Context> _cachedContext;
public Context Instance
{
get
{
Context context;
if (!_cachedContext.TryGetTarget(out context))
{
context = new Context();
_cachedContext.SetTarget(context);
}
return context;
}
}
}
This code is planned to be used without IDisposable.Dispose calling in the client-side. What problems this can cause except singleton (anti)pattern? Thanks.