I am trying to catch the exception when the Database is unavailable in WPF with EF. I am using MVVM and Repo pattern with IUnityContainer in C#.
My problem is that if the database is unavailable the program crashes on InitializeComponent() statement in the code behind of the View. I have tried searching on catching Exceptions and Error Handling etc. and most of the suggestions center around Try Catch logic, which is to be expected. I have tried to wrap statement in a try-catch block as below but it still crashes in the same place on InitalizeComponent.
Public MyListView() {
try {
IntializeComponent();
} catch (Exception) {
throw;
}
}
I have also tried to add Try-Catch Blocks in various other points of my code such as where the DB is Initialised:
Database.SetInitializer(new DataInitialiser());
Where the Unity container is registered:
_container.RegisterType<IRepo<MyList>, MyListRepo>(new TransientLifetimeManager());
and where the data is loaded:
MyLists = new ObservableCollection<MyList>(await _repo.GetAllAsync());
I would like to preserve the MVVM pattern so catch the exception and provide an elegant response to the user from within the ViewModel. So my specific question is where can I catch the Exception when the database is unavailable.
Thanks in advance.