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 ...
Get Programming Entity Framework: DbContext now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.