Default behavior of a transaction
EF supports transactions out of the box, all we need to do is perform SaveChanges()
only once; it saves changes only if the transaction has been executed successfully, otherwise, the transactions will be rolled back automatically.
Let's investigate how we could practically leverage the default transactional behavior in our blogging system:
- We need two entities that need to be updated in a single web request
- Both the entities should be added/updated in the data context
- With a single
SaveChanges()
, both entities will be updated in the data store
In the blogging system, let's include support to add one or more tags in posts and learn about default transaction support in parallel with the Tags
integration in posts.
Adding tags support in the blogging system
The fields required in the Post
entity to incorporate Tags
support in posts will be covered in this section. Let's start the activity by including the fields required to persist tag information in the Post
model...