Deleting Cars from the Database
This article is relevant to entity models that utilize the deprecated Visual Studio integration of Telerik Data Access. The current documentation of the Data Access framework is available here.
In this step you will add functionality to MainViewModel which will allow you to delete cars from the database. You will need to implement the methods required for that and define properties which expose commands based on them.
Defining the required methods
-
In MainViewModel add a new method called DeleteCar. This method displays a confirmation message box and if the delete operation is confirmed, the currently selected car is deleted from the database. It will be used to form the delete command.
private void DeleteCar() { Car carToDelete = this.SelectedCar; MessageBoxResult deleteConfirmation = MessageBox. Show(string.Format(CONFIRM_DELETE_MESSAGE, carToDelete.Make, carToDelete.Model), CONFIRM_DELETE_TITLE, MessageBoxButton.YesNo, MessageBoxImage.Question); if (deleteConfirmation != MessageBoxResult.Yes) { return; } this.context.Delete(carToDelete); bool saveChangesIsSuccessful = false; try { this.context.SaveChanges(); saveChangesIsSuccessful = true; } catch (DataStoreException ex) { MessageBox.Show(DELETE_ERROR_RELATED_ENTRY_MESSAGE + ex.BackendError.Description, DELETE_ERROR_TITLE, MessageBoxButton.OK, MessageBoxImage.Error); } catch (Exception ex) { MessageBox.Show(DELETE_ERROR_MESSAGE + ex.Message, DELETE_ERROR_TITLE, MessageBoxButton.OK, MessageBoxImage.Error); } if (saveChangesIsSuccessful) { this.RetrieveCarsToDisplay(); } }
Private Sub DeleteCar() Dim carToDelete As Car = Me.SelectedCar Dim deleteConfirmation As MessageBoxResult = MessageBox. Show(String.Format(CONFIRM_DELETE_MESSAGE, carToDelete.Make, carToDelete.Model), CONFIRM_DELETE_TITLE, MessageBoxButton.YesNo, MessageBoxImage.Question) If deleteConfirmation <> MessageBoxResult.Yes Then Exit Sub End If Me._context.Delete(carToDelete) Dim saveChangesIsSuccessful As Boolean = False Try Me._context.SaveChanges() saveChangesIsSuccessful = True Catch ex As DataStoreException MessageBox.Show(DELETE_ERROR_RELATED_ENTRY_MESSAGE + ex.BackendError.Description, DELETE_ERROR_TITLE, MessageBoxButton.OK, MessageBoxImage.Error) Catch ex As Exception MessageBox.Show(DELETE_ERROR_MESSAGE + ex.Message, DELETE_ERROR_TITLE, MessageBoxButton.OK, MessageBoxImage.Error) End Try If saveChangesIsSuccessful Then Me.RetrieveCarsToDisplay() End If End Sub
-
In MainViewModel add a new method called IsCarSelected. This method will be used to determine if the delete command can be executed. The delete command can be executed only when there is a selected car available.
private bool IsCarSelected() { return this.SelectedCar != null; }
Private Function IsCarSelected() As Boolean Return IsNothing(Me.SelectedCar) = False End Function
Exposing commands to the view
In MainViewModel define a property DeleteCommand. In its getter initialize a RelayCommand object based on the DeleteCar and IsCarSelected methods and return it.
public RelayCommand DeleteCarCommand
{
get
{
this.command = new RelayCommand(this.DeleteCar, this.IsCarSelected);
return this.command;
}
set
{
this.command = value;
}
}
Public Property DeleteCarCommand As RelayCommand
Get
Me._command = New RelayCommand(AddressOf Me.DeleteCar, AddressOf Me.IsCarSelected)
Return Me._command
End Get
Set(value As RelayCommand)
Me._command = value
End Set
End Property
Checkpoint
The sample application can now delete cars from the database.