Chapter 3. Adding, Changing, and Deleting Entities
In the previous chapter you saw how to get data from the database into memory. But this is only half the story. Most applications also need to make changes to that data and then push those changes back into the database. In this chapter we will take a look at how Entity Framework can be used to make changes to data. These changes fall into three main categories: adding new data, changing existing data and deleting existing data.
While looking at querying, we saw the main benefit of using an Object
Relational Mapper (ORM), like Entity Framework, is that application code is
written in terms of your object model. As you write your application, you
don’t need to be looking at the shape of your tables and columns. Nor do you
need to know how to write INSERT,
UPDATE, and DELETE statements for your database. Entity
Framework will take care of translating the operations you perform on your
objects into SQL statements that will push these changes into the
database.
As you perform operations on your object instances, Entity Framework
uses its change tracker to keep track of what you have
done. When you’re ready to commit the changes to the database, you call the
SaveChanges method. SaveChanges will invoke the update
pipeline, which is responsible for translating the changes to
your object instances into SQL statements that are executed against your
database. If you’ve developed applications using Entity Framework’s ObjectContext, you should ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access