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