public View GetViewDeep(int id, CMSContext db = null) { bool isFirstCall = (db == null) ? true : false; try { if (isFirstCall) { db = new CMSContext(); } var view = db.Views .Include(v => v.Template) .Include(v => v.ChildViews) .Include(v => v.Filters) .Include(v => v.Filters.Select(f => f.FilterFields)) .Include(v => v.Filters.Select(c => c.FilterFields.Select(cf => cf.Field))) .Include(v => v.Filters.Select(c => c.FilterFields.Select(cf => cf.Field.Dictionary))) .Include(v => v.LinkedField) .Include(v => v.Style) .Include(v => v.Controls) .Include(v => v.Controls.Select(c => c.Style)) .Include(v => v.Controls.Select(c => c.ControlFields)) .Include(v => v.Controls.Select(c => c.ControlFields.Select(cf => cf.Field))) .Include(v => v.Controls.Select(c => c.ControlFields.Select(cf => cf.Field.Dictionary))) .Include(v => v.Controls.Select(c => c.ControlFields.Select(cf => cf.Field.LinkedField))) .Include(v => v.Controls.Select(c => c.ControlFields.Select(cf => cf.Field.Template))) .Include(v => v.Controls.Select(c => c.Events)) .Include(v => v.Controls.Select(c => c.Events.Select(e => e.Actions))) .Include(v => v.Controls.Select(c => c.Events.Select(e => e.Actions.Select(a => a.Parameters)))) .FirstOrDefault(v => v.Id == id); if (view.ParentView == null) { view.RootView = view; } else { var curView = view; while (curView.ParentView != null) { curView = curView.ParentView; } view.RootView = curView; } var childViews = view.ChildViews.ToList(); for (int i = 0; i < childViews.Count; i++) { GetViewDeep(childViews[i].Id, db); } return(view); } finally { if (isFirstCall) { db.Dispose(); } } }