We have seen the default behavior of a transaction if the multiple persistence occurs within a single SaveChanges(), what if we have a business requirement where multiple SaveChanges() are required and we still need to maintain the transaction scope? We have a provision to explicitly define a transaction and bring in the changes required inside this scope; EF exposes the Database type in Context, which has the BeginTransaction() functionality.
Let's try to achieve the aforementioned functionality in our CreatePostCommand type and still maintain the transaction scope. We could perform the same by retrieving the transaction object using the BeginTransaction() function; the retrieved object could be used either to commit or roll back the changes. The following code commits the changes after persisting the Post and Tag objects:
public...