I have a razor layout like:
@using (var context = SetUpSomeContext()) { Some content here @RenderBody(); } <
If you require some logic on all your views, create a ViewModelBase that all your ViewModels inherit from.
ViewModelBase
ViewModel
Then in your Controller(Base) you can initialize the ViewModel.SharedContext and other properties.
Controller(Base)
ViewModel.SharedContext