Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
Let's look at updating entities. Specifically, updating ComicBook entities in the Repository class' `UpdateComicBook` method.
Follow Along
To follow along commiting your changes to this course, you'll need to fork the dotnet-comic-book-gallery-model repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork>
cd dotnet-comic-book-gallery-model
git checkout tags/v5.3 -b updating-entities
-
0:00
Let's look at updating entities, specifically updating
-
0:03
comic book entities in the repository classes UpdateComicBook method.
-
0:12
As mentioned in the previous video,
-
0:14
EF tracks the changes to entities that are loaded into the context.
-
0:18
The UpdateComicBook method is past an instance of a comic book entity, but
-
0:23
that entity is not being tracked by the context.
-
0:26
Entities that are not being tracked by the context are said to be disconnected or
-
0:31
detached.
-
0:33
There are a number of ways that we can persist data changes contained within
-
0:36
disconnected or detached entities.
-
0:39
Our first option is to retrieve the entity from the database.
-
0:43
Then write code to manually update the property values.
-
0:47
To retrieve the entity, we can use the DB set find method.
-
0:52
Don't forget, we need to start with getting an instance of the context.
-
0:55
Using (Context context
-
1:01
= GetContext()).
-
1:06
Then we can retrieve the comic book,
-
1:12
ComicBook comicBookToUpdate=
-
1:17
Context.ComicBooks.Find(comicBook.Id).
-
1:26
Then we simply update the retrieved entity property values with the values from
-
1:31
the passed in entity.
-
1:35
comicBookToUpdate.SeriesId = comicBook.seriesId.
-
1:43
comicBookToUpdate.IssueNumber =
-
1:49
comicBook.IssueNumber.
-
1:54
comicBookToUpdate.Descriptions =
-
2:02
comicBook.Description.
-
2:07
comicBookToUpdate.PublishedOn =
-
2:12
comicBook.PublishedOn.
-
2:17
And then comicBookToUpdate.AverageRating
-
2:24
= comicBook.AverageRating, and call the SaveChanges method.
-
2:33
When we call the SaveChanges method,
-
2:35
EF will detect the changes to the entity that is being tracked in the context.
-
2:40
Generate a sequel update statement and
-
2:43
execute that statement against the database.
-
2:46
All this approach is simple and
-
2:48
easy to understand, it's tedious to write and maintain.
-
2:52
Let's look at another option.
-
2:56
In this option,
-
2:57
we still use the DB set find method to retrieve the entity from the database.
-
3:02
But instead of manually setting the new property values,
-
3:06
we retrieve the entity's entry from the context.
-
3:09
And call the current value's set values method to copy the passed in entity's
-
3:14
property values to the entry's current values collection.
-
3:17
context.Entry(ComicBookToUpdate).CurrentV-
-
3:25
alues.SetValues, then pass
-
3:30
in the (comicBook) parameter.
-
3:35
After calling the sub values method, the entity state assuming that at least
-
3:40
one property value is updated, will be set to modified.
-
3:44
A state of modified indicates that the entity has a change
-
3:48
that hasn't been persisted to the database.
-
3:51
When we call the SaveChanges method, EF will generate a SQL update statement for
-
3:56
the modified entity and execute that statement against the database.
-
4:00
Let's test our changes.
-
4:02
Set a break point just after the call to the save changes method and run the app.
-
4:12
Enter 3 to view the detail for issue number 3 of the bone series.
-
4:17
Then enter U to update the comic book.
-
4:21
Let's change the average rating.
-
4:23
Enter 5 to select a property, then enter 7 for the value.
-
4:30
Enter S to save the comic book.
-
4:34
Now, we're at our break point.
-
4:36
The save changes method was just called.
-
4:50
In the output window, we can see the queries that were generated and executed.
-
4:55
The first query was the query to retrieve the comic book entity.
-
5:06
And the second query was to update the average rating column.
-
5:10
This option is an improvement over the first.
-
5:13
And that we don't have to manually update the entity properties.
-
5:17
A downside to both approaches is that we have to retrieve the entity
-
5:20
from the database.
-
5:22
Which means that updating an entity requires two queries.
-
5:26
One to retrieve the entity and
-
5:28
another to persist any changes to the entity to the database.
-
5:37
Let's look at an option that doesn't require two queries.
-
5:41
Instead of retrieving the entity from the database,
-
5:44
we can attach the passed-in entity to the context using the DB set attach method.
-
5:50
context.ComicBooks.Attach(comicBook).
-
5:59
After attaching the entity to the context, its state will be set to unchanged.
-
6:04
EF is unable to detect whether or not any of the entity's property values
-
6:09
are different from the values that are currently in the database.
-
6:13
We can force EF to treat the entity's values as new
-
6:17
values by setting the associated entry state to modified.
-
6:22
context.Entry(comicBook).State = EntityState.Modified.
-
6:34
Now, when we call the SaveChanges method,
-
6:37
EF will persist each of the entity's property values to the database.
-
6:41
Let's test our changes again.
-
6:47
Enter 3 to view the detail for issue number 3 of the bone series.
-
6:53
Then enter U to update the comic book.
-
6:56
Let's change the description.
-
6:58
Enter 3 to select the property.
-
7:02
This is my new description.
-
7:08
Then enter S to save the comic book.
-
7:12
We're at our break point again, just after the SaveChanges method call.
-
7:17
In the output window, we can see the query that was generated and executed.
-
7:25
So we were able to eliminate the query to retrieve the entity from the database.
-
7:30
But on the downside, as we can see in the generated query,
-
7:34
every column value is being updated in the database.
-
7:37
Not just the ones that have actually changed.
-
7:44
In most situations, this approach works fine.
-
7:47
Though, there might be times that an entity contains a property that shouldn't
-
7:52
ever be updated.
-
7:53
Let's imagine that once a comic book has been added to the database,
-
7:56
its issue number shouldn't be changed.
-
8:00
Let's start with rewriting this line of code, so
-
8:03
that we retrieve the current books entry into a variable.
-
8:09
Var comicBookEntry = context.Entry(comicBook).
-
8:21
Then, comicBookEntry.State
-
8:26
= EntityState.Modified.
-
8:31
Then use the entries property method
-
8:33
to get a reference to the issued number properties entry object.
-
8:37
And set the entries is modified property to false.
-
8:42
comicBookEntry.Property("IssueNumber").Is-
-
8:50
Modified = false;.
-
8:54
This line of code will prevent the IssueNumber property from being included
-
8:58
in the generated SQL update query.
-
9:00
Go ahead and remove or comment out this line of code.
-
9:04
We want our users to be able to change a comicBook's issue number.
-
9:08
After a code challenge, we'll see how to delete entities
You need to sign up for Treehouse in order to download course files.
Sign up