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 see how we can use Code First Migrations to seed our database with lookup and test data.
Follow Along
To follow along commiting your changes to this course, you'll need to fork the dotnet-ef-migrations repo. Then you can clone, commit, and push your changes to your fork like this:
git clone <your-fork>
cd dotnet-ef-migrations
git checkout tags/v1.5 -b populating-our-database-with-seed-data
Code
You can download the C# seed test data from this GitHub Gist.
https://gist.github.com/smashdevcode/a4962b1a28365a74abc00a0b5d0ea79a
-
0:00
We have our database, but now we need to populate it was some data.
-
0:04
This is commonly referred to as seeding a database.
-
0:08
Seed data can be faked or dummy data to assist us with the development and
-
0:13
testing of our application.
-
0:15
Or it can be data that the application requires in order to operate properly.
-
0:21
Our comic book gallery model requires us to specify the role that an artist has for
-
0:26
a comic book that they are associated with.
-
0:28
To do this, we need the role table to be populated with a set of values
-
0:33
that we can select from.
-
0:35
This kind of data is sometimes referred to as Lookup Data.
-
0:39
In the comic book artist table, we have a role ID column
-
0:42
which provides us with a primary key value for a row in the role table.
-
0:47
With that primary key value in hand, we can query or
-
0:52
look up the associated role name column value.
-
0:56
Let's see how we can use code first migrations to seed our database.
-
1:00
Our configuration class inherits from the code first migrations,
-
1:04
DbMigrationsConfiguration base class,
-
1:07
which provides a seed method that we can override and use to seed our database.
-
1:12
The configuration class that was generated for
-
1:14
us when we enabled migrations has already done this.
-
1:20
The seed method has passed an instance of our context class
-
1:24
which we can use to reference the roles Db set property.
-
1:29
The Db set class contains an outer update method
-
1:33
that can be used to only insert data if it doesn't exist in the database.
-
1:38
If the data is already in the database then an update will be performed.
-
1:42
Developers commonly referred to this operation as an up cert.
-
1:46
This is helpful as the seed method will be called every time after a migration has
-
1:51
been applied to the database using the Update-Database command.
-
1:58
The first parameter that we need to pass to the outer update method
-
2:02
is a lambda expression that specifies the properties, that EF should
-
2:06
use when determining whether an add or update operation should be performed.
-
2:11
Each row should have a unique name.
-
2:13
So, we can use the name property as a natural key, r goes to r.Name.
-
2:27
Then, we can pass one or more row
-
2:32
entity object instances to add or
-
2:37
update, new Role, Name = "Script".
-
2:47
Don't forget to add a using directive for
-
2:49
the ComicBookGalleryModel.Models namespace.
-
2:55
Then, let's add a second role,
-
3:01
new Role, Name = "Pencils".
-
3:06
The context's save changes method is automatically called, so
-
3:10
there's no need to call it directly from the seed method.
-
3:14
Now, let's run our application.
-
3:16
Make sure you have a breakpoint set in the program.cs file
-
3:20
on the query that retrieves the list of comic books.
-
3:27
We're at our breakpoint.
-
3:29
Press F5 and then Enter to continue execution.
-
3:37
Now let's use the SQL Server Object Explorer Window to view the data in
-
3:42
the role table.
-
3:43
Right-click on the role table and select view data.
-
3:50
And the table doesn't contain any data, why is this?
-
3:56
Changes that you make to the seed method won't take effect
-
3:59
until a migration is applied using the update-database command.
-
4:03
We don't have a migration to apply, but that's okay.
-
4:07
We can still run the update-database command, which will determine that there
-
4:10
aren't any pending migrations to apply, and then call the seed method.
-
4:14
Update-database.
-
4:26
We can see here in the console output that the seed method was called.
-
4:32
Right-click on the role table and select view data again.
-
4:39
And now we're seeing our C data.
-
4:42
Let's look at an example of adding some test data
-
4:45
that utilizes our role lookup data.
-
4:49
To save us some time, I already have our C data code here in notepad.
-
4:54
If you're following along, you can find a link to this code in the teacher's notes.
-
4:58
I'll select all, copy to the clipboard, and
-
5:02
then paste into the seed method replacing our previous implementation.
-
5:25
Notice that we've updated our Role C data, to use the roleId property for
-
5:30
the property that EF should use to determine if this should be an add or
-
5:34
update.
-
5:36
And we updated the role entity object instances to have explicit
-
5:41
Id property values, which are set to the constant integer values defined here.
-
5:47
By switching to this approach we now have easy access to the roleId values
-
5:52
when associating an artist with a comic book.
-
5:55
Which we'll see an example of in just a bit.
-
6:00
Here we're adding two artists, Stan Lee and Steve Ditko, and
-
6:05
a little further down we're adding a series, The Amazing Spider-Man.
-
6:11
And finally, here's where we're adding our first comic book.
-
6:17
Notice how we're making use of the available foreign key SeriesId property to
-
6:22
associate the series.
-
6:27
And, here we're calling the comic book entities AddArtist method,
-
6:32
which allows us to pass the artistId to add along with the roleId for their role.
-
6:41
We put our cursor on the AddArtist method and press alt F12 to pick the definition.
-
6:46
We can see that the parameter values flow to the corresponding comic book
-
6:51
artist entity class foreign key properties.
-
6:55
Using the package manager console, let's run the update-database command to update
-
7:00
our database with our new test data.
-
7:07
Update-database and press enter,
-
7:12
and run our app again.
-
7:18
Press F5 to continue execution.
-
7:23
And here's our test data displayed in the console.
-
7:27
Press enter to continue execution.
You need to sign up for Treehouse in order to download course files.
Sign up