When you're pulling data in and out of a database, it's important to keep track of what you've changed; otherwise, that data won't be written back into the database.
One of the most common design patterns that helps to form the unit, which is responsible for data persistance is the Unit of Work.
A Unit of Work keeps track of everything you do during a business transaction that can affect the database. When you're done, it figures out everything that needs to be done to alter the database as a result of your work.
The key thing about Unit of Work is that when it comes time to commit, the Unit of Work decides what to do. It carries out the inserts, updates, and deletes in the right order.
An article in the following link, discusses various aspects of this pattern and examines the issues around persistence ignorance.