Making a Model Change7:43 with James Churchill
Let's make a simple change to our model and add a new migration for that model change.
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/v2.1 -b making-a-model-change
[SOUND] Adding your initial migration is something 0:00 that you'll only do once per project. 0:05 But every time that you make a change to your model, 0:09 you'll need a corresponding migration. 0:11 Let's make a simple change to our model and add a new migration for 0:14 that model change. 0:17 Open the artist.cs file in the Models folder and 0:19 add a bio property to the artist entity class. 0:24 I'll use the prop code snippet, prop press the Tab key twice. 0:32 Then change the data type to string. 0:39 Press the Tab key twice, then change the property name to Bio. 0:41 Press the Escape key to exit the code snippet. 0:47 Open the NuGet Package Manager console, 0:50 Tools > NuGet Package Manager > Package Manager Console. 0:54 And run the add migration command. 1:02 Add-migration. 1:05 For the migration name, I like to make the name as descriptive as possible. 1:09 AddBioPropertyToArtist. 1:13 Feel free to use whatever naming convention you or 1:19 your team want to use, but as always, consistency is important. 1:22 Press Enter to run the command. 1:27 Once the command is completed, we can see the file that EF added to our project. 1:31 As we saw in an earlier video, 1:37 the filename is the name of our migration prefixed with a timestamp. 1:39 This ensures that our latest migration will always be displayed 1:44 below in existing migrations. 1:48 Let's review the migration class. 1:50 Our model change was a simple one. 1:56 We just added a single property to a single entity class. 1:59 So our migration class is also unsurprisingly simple. 2:03 In the Up method, 2:07 the add column method is being used to add a bio column to the Artist database table. 2:09 The first parameter is the table name. 2:16 The second parameter is the name of the column to add. 2:19 And the third printer is a lambda that allows us to configure the column. 2:23 Since our new column allows nulls and can contain a string of any length, well, 2:28 up to the maximum allowable string size. 2:33 Our configuration consist entirely of a call to the column builder string method. 2:36 In the down method, 2:43 the DropColumn method is being used to drop the bio column from the Artist table. 2:44 Let's add a required data annotation attribute to the bio property 2:49 in order to ensure that artists always have bios. 2:53 Bracket Required. 2:59 And let's run the add migration command again to update the migration. 3:02 We can press the up arrow key to retrieve the command that was previously executed. 3:06 Since we're updating an existing migration, 3:11 we need to supply the force flag or we'll get an error. 3:14 Space-force, press Enter to run the command. 3:17 We'll be prompted if we want to reload the migration file that we 3:23 have open in the editor. 3:28 I'll click Yes so we can review the changes that were made to the file. 3:30 In the Up method, we can now see that the bio column configuration 3:39 has been updated to not allow nulls. 3:42 Let's try making another model change and add in another migration. 3:47 In the series entity class, add a publisher property. 3:59 String Publisher. 4:08 Then run the add-migration command to add a migration for this model change. 4:13 Add-migration AddPublisherPropertyToSeries. 4:19 And we get an error. 4:32 Unable to generate an explicit migration because the following explicit migrations 4:36 are pending, a timestamp followed by AddBioPropertyToArtist. 4:41 Apply the pending explicit migrations 4:46 before attempting to generate a new explicit migration. 4:48 So this error is telling us that we can only have one pending migration at a time. 4:52 On the surface, this might seem like a frustrating limitation. 4:56 But in reality, when you're making model changes, 5:00 you're typically working on implementing a new feature or resolving a bug. 5:03 And the model changes that you'll make are okay to roll up into a single migration. 5:09 In fact, having a single migration for 5:14 a related set of model changes makes them easier to review and reason about. 5:17 Let's undo our changes to the series in the class 5:23 by removing the publisher property. 5:25 Now, let's update our database. 5:37 Update-database and we get an error. 5:40 If we scroll up to the console output, we can see that the migration was applied. 5:50 And the Seed method was called and then an exception was thrown. 5:55 Validation failed for one or more entities, 6:06 the only change that we made was a change to our artist entity. 6:09 So let's review our artist data. 6:14 Remember, the db AddOrUpdate method will update records if they exist in 6:25 the database. 6:30 Since we've previously seeded our database with our artist records. 6:32 This call to AddOrUpdate method is attempting to update our artists and 6:35 it's failing. 6:40 Because we're not supplying the newly added required bio property. 6:41 Let's update both artist entity object instances to have bio property values. 6:46 For now, I'll just set their values to TBD 6:51 ,Bio = TBD, then the second 6:59 artist Bio = TBD. 7:07 Then run the update database command again. 7:11 This time we didn't get an error. 7:22 Let's review the Artist table data. 7:25 And here's our two records with their TBD Bio column values. 7:38
You need to sign up for Treehouse in order to download course files.Sign up