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.
Visual Studio 2026. Version 18.0.0 or above. It will work with the free community version.
.NET 10 SDK. Version 10.0.0 or above.
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 ConnectionStrings > EFTransactionsDbContext 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.
Download this code example
Enter your email and we'll send you a download link.
Related code examples
Download a .NET 10 Web API integrated with Minimal APIs and SQL Server alongside Entity Framework Core and FluentValidation.
Learn how to add Entity Framework Core migrations in an Web API and worker project and use it to automate SQL scripts across all environments.
Download this code example
Enter your email and we'll send you a download link.