The JoobContext object has a method for beginning a transaction. The method returns a transaction object with methods for committing or rolling back a transaction. The transaction object must be disposed of at the end of the transaction.
The simplest syntax for carrying out a transaction uses a using statement that implicitly calls the Dispose method on the transaction object at the end of the block.
The SetPropertiesOnCreate method, which was exposed by the C# Exposure wizard, sets properties of the Customer object from values entered in the text boxes on the form.
If the statement calling the Commit method is omitted or not executed because an exception is thrown, the transaction is automatically rolled back. With a using statement, you do not have to explicitly call the Rollback method.
In the MainWindow.xaml form, double‑click the Add button. This adds a click event method in the MainWindow.xaml.cs code file.
Add the following code to the btnAdd_Click method.
private void btnAdd_Click(object sender, RoutedEventArgs e) { using (var tran = context.BeginTransaction()) { Customer cust = new Customer(); cust.SetPropertiesOnCreate(txtAddress.Text, txtFirstNames.Text, txtLastName.Text); tran.Commit(); } }
Run the BankingApp application and the enter data for a new customer. Click the Add button to add the customer and then click the Refresh button to display the new customer.