/// <summary> /// Close all db context /// </summary> public static void CloseAllDbContext() { HttpContext context = HttpContext.Current; if (context != null && context.Session != null) { EFDbContextStorage ctxStorage = context.Session[STORAGE_KEY] as EFDbContextStorage; if (ctxStorage != null) { foreach (EFDbContext ctx in ctxStorage.GetAllDbContexts()) { if (ctx.Database.Connection.State == System.Data.ConnectionState.Open) { ctx.Database.Connection.Close(); } if (ctx.ObjectContext.Connection.State == System.Data.ConnectionState.Open) { ctx.Database.Connection.Close(); } ctx.ObjectContext.Dispose(); ctx.Dispose(); } context.Session.Remove(STORAGE_KEY); } } }
/// <summary> /// Get all DB Context /// </summary> /// <returns></returns> public static IEnumerable <EFDbContext> GetAllEFDbContext() { HttpContext context = HttpContext.Current; if (context != null && context.Session != null) { EFDbContextStorage ctxStorage = context.Session[STORAGE_KEY] as EFDbContextStorage; return(ctxStorage.GetAllDbContexts()); } else { return(null); } }