Using transactions with Entity Framework Core

This Entity Framework Core sample project uses transactions to add an order to a dispatch queue and mark it as paid.

You can find out more about transactions in Entity Framework Core, or watch the video:

Download the code example

You'll need to fill out the code example form. We will send you an email where you can download the code example.

Software

This is the software that will need to be installed onto your machine.

Import the database

This application integrates with a SQL Server database. Inside the Database folder, there is RoundTheCode_EFCoreTransactions.bacpac and RoundTheCode_EFCoreTransactions.bak. Use either of these database backups to import the database to your local SQL Server database server.

The application

Inside Api/appsettings.Development.json, find ConnectionStringsEFTransactionsDbContext and point the connection string to your imported database.

Open up RoundTheCode.EFTransactions.slnx, and run the application. The Swagger documentation will load at https://localhost:7401/swagger with this endpoint:

  • /api/orders/{id}/mark-paid (POST) - Adds the order to the dispatch queue table and marks the order as paid. For {id}, use ID for any record in the [dbo].[Orders] table that has not been marked as paid.

Reset data

To reset [dbo].[DispatchQueue] and [dbo].[Orders], run ResetOrders.sql inside the Database folder against your database. This will revert [dbo].[DispatchQueue] and [dbo].[Orders] back to the original data.