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 expand the data set that we're seeding our database with so that we have more data to work with.
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/v3.8 -b populating-your-database-with-seed-data
Code
Here's the code for our expanded test data.
var seriesSpiderMan = new Series()
{
Title = "The Amazing Spider-Man",
Description = "The Amazing Spider-Man (abbreviated as ASM) is an American comic book series published by Marvel Comics, featuring the adventures of the fictional superhero Spider-Man. Being the mainstream continuity of the franchise, it began publication in 1963 as a monthly periodical and was published continuously, with a brief interruption in 1995, until its relaunch with a new numbering order in 1999. In 2003 the series reverted to the numbering order of the first volume. The title has occasionally been published biweekly, and was published three times a month from 2008 to 2010. A film named after the comic was released July 3, 2012."
};
var seriesIronMan = new Series()
{
Title = "The Invincible Iron Man",
Description = "Iron Man (Tony Stark) is a fictional superhero appearing in American comic books published by Marvel Comics, as well as its associated media. The character was created by writer and editor Stan Lee, developed by scripter Larry Lieber, and designed by artists Don Heck and Jack Kirby. He made his first appearance in Tales of Suspense #39 (cover dated March 1963)."
};
var seriesBone = new Series()
{
Title = "Bone",
Description = "Bone is an independently published comic book series, written and illustrated by Jeff Smith, originally serialized in 55 irregularly released issues from 1991 to 2004."
};
var artistStanLee = new Artist()
{
Name = "Stan Lee"
};
var artistSteveDitko = new Artist()
{
Name = "Steve Ditko"
};
var artistArchieGoodwin = new Artist()
{
Name = "Archie Goodwin"
};
var artistGeneColan = new Artist()
{
Name = "Gene Colan"
};
var artistJohnnyCraig = new Artist()
{
Name = "Johnny Craig"
};
var artistJeffSmith = new Artist()
{
Name = "Jeff Smith"
};
var roleScript = new Role()
{
Name = "Script"
};
var rolePencils = new Role()
{
Name = "Pencils"
};
var comicBook1 = new ComicBook()
{
Series = seriesSpiderMan,
IssueNumber = 1,
Description = "As Peter Parker and Aunt May struggle to pay the bills after Uncle Bens death, Spider-Man must try to save a doomed John Jameson from his out of control spacecraft. / Spideys still trying to make ends meet so he decides to try and join the Fantastic Four. When that becomes public knowledge the Chameleon decides to frame Spider-Man and steals missile defense plans disguised as Spidey.",
PublishedOn = new DateTime(1963, 3, 1),
AverageRating = 7.1m
};
comicBook1.AddArtist(artistStanLee, roleScript);
comicBook1.AddArtist(artistSteveDitko, rolePencils);
context.ComicBooks.Add(comicBook1);
var comicBook2 = new ComicBook()
{
Series = seriesSpiderMan,
IssueNumber = 2,
Description = "The Vulture becomes the city's most feared thief. Spider-Man must find a way to figure out his secrets and defeat this winged menace. / Spider-Man is up against The Tinkerer and a race of aliens who are trying to invade Earth.",
PublishedOn = new DateTime(1963, 5, 1),
AverageRating = 6.8m
};
comicBook2.AddArtist(artistStanLee, roleScript);
comicBook2.AddArtist(artistSteveDitko, rolePencils);
context.ComicBooks.Add(comicBook2);
var comicBook3 = new ComicBook()
{
Series = seriesSpiderMan,
IssueNumber = 3,
Description = "Spider-Man battles Doctor Octopus and is defeated, he considers himself a failure until the Human Torch gives a speech to his class which inspires him to go in prepared and win the day, leaving Doctor Octopus under arrest. Spider-Man visits the Torch with words of thanks.",
PublishedOn = new DateTime(1963, 7, 1),
AverageRating = 6.9m
};
comicBook3.AddArtist(artistStanLee, roleScript);
comicBook3.AddArtist(artistSteveDitko, rolePencils);
context.ComicBooks.Add(comicBook3);
var comicBook4 = new ComicBook()
{
Series = seriesIronMan,
IssueNumber = 1,
Description = "A.I.M. manages to make three duplicates of the Iron Man armor.",
PublishedOn = new DateTime(1968, 5, 1),
AverageRating = 7.6m
};
comicBook4.AddArtist(artistArchieGoodwin, roleScript);
comicBook4.AddArtist(artistGeneColan, rolePencils);
context.ComicBooks.Add(comicBook4);
var comicBook5 = new ComicBook()
{
Series = seriesIronMan,
IssueNumber = 2,
Description = "Stark competitor Drexel Cord builds a robot to destroy Iron Man.",
PublishedOn = new DateTime(1968, 6, 1),
AverageRating = 6.7m
};
comicBook5.AddArtist(artistArchieGoodwin, roleScript);
comicBook5.AddArtist(artistJohnnyCraig, rolePencils);
context.ComicBooks.Add(comicBook5);
var comicBook6 = new ComicBook()
{
Series = seriesIronMan,
IssueNumber = 3,
Description = "While helping Stark, Happy once again turns into the Freak.",
PublishedOn = new DateTime(1968, 7, 1),
AverageRating = 6.8m
};
comicBook6.AddArtist(artistArchieGoodwin, roleScript);
comicBook6.AddArtist(artistJohnnyCraig, rolePencils);
context.ComicBooks.Add(comicBook6);
var comicBook7 = new ComicBook()
{
Series = seriesBone,
IssueNumber = 1,
Description = "Fone Bone, Smiley Bone and Phoney Bone are run out of Boneville and get separated in the desert. Fone Bone finds his way to the valley.",
PublishedOn = new DateTime(1991, 7, 1),
AverageRating = 7.1m
};
comicBook7.AddArtist(artistJeffSmith, roleScript);
comicBook7.AddArtist(artistJeffSmith, rolePencils);
context.ComicBooks.Add(comicBook7);
var comicBook8 = new ComicBook()
{
Series = seriesBone,
IssueNumber = 2,
Description = "While babysitting Miz Possum's children, Bone is chased by Rat Creatures, is saved by the Dragon and finally finds Thorn.",
PublishedOn = new DateTime(1991, 9, 1),
AverageRating = 6.9m
};
comicBook8.AddArtist(artistJeffSmith, roleScript);
comicBook8.AddArtist(artistJeffSmith, rolePencils);
context.ComicBooks.Add(comicBook8);
var comicBook9 = new ComicBook()
{
Series = seriesBone,
IssueNumber = 3,
Description = "Grandma Ben returns from Barrelhaven and Phoney Bone and Bone reunite.",
PublishedOn = new DateTime(1991, 12, 1),
AverageRating = 6.9m
};
comicBook9.AddArtist(artistJeffSmith, roleScript);
comicBook9.AddArtist(artistJeffSmith, rolePencils);
context.ComicBooks.Add(comicBook9);
context.SaveChanges();
Additional Learning
-
0:00
When we initially populate our database with data,
-
0:03
we say that we're seeding our database.
-
0:05
Sometimes the seed data is fake or
-
0:08
dummy data that enables the development or testing of the system.
-
0:12
Sometimes, it's data that needs to be present in order for
-
0:15
the system to operate correctly.
-
0:18
In the next two sections, we'll be looking at how to use LINQ with entity framework
-
0:22
to write queries and perform CRUD operations.
-
0:26
To prepare for that,
-
0:27
it'll be helpful to expand the dataset that we're seeding our database with.
-
0:31
So that we have more data to work with,
-
0:34
instead of expanding the code that we've been writing in the program.CS file.
-
0:38
Let's see how we can use a custom EF database initializer to seed our database.
-
0:44
Right-click on the project and select Add>Class.
-
0:50
Name the class Database, Initializer and click Add.
-
0:59
By default, a class without an access modifier is only
-
1:02
visible to other members within the same project.
-
1:06
For the purposes of our database initializer class that will work fine,
-
1:10
as only our context class will need access.
-
1:13
We can be explicit by using the internal access modifier.
-
1:18
To create a custom database initializer,
-
1:20
we can inherit from one of the provided EF database initialize your classes.
-
1:25
Let's use the drop create database if model changes class.
-
1:29
So we're only dropping and creating the database if our model changes.
-
1:34
Add a using statement for the system.Data.Entity namespace.
-
1:40
And set the base class to the DropCreateDatabaseIfModelChanges class.
-
1:48
DropCreateDatabaseIfModelChanges of type context.
-
1:54
Now we override the base classes seed method.
-
1:59
The seed method is just called after the database has been created which
-
2:03
is a convenient time to see the database.
-
2:11
We also need to update our context class constructor to use our custom database
-
2:16
initializer.
-
2:19
So, Database.SetInitializer(new DatabaseInitializer()).
-
2:30
Now we need to move our test data from the program.cs file to our new seed method.
-
2:37
Starting just inside the using statement.
-
2:39
Select all of the code down to and including the call to the context is
-
2:44
saved changes method and cut the selection to the clipboard.
-
2:55
Let's remove the call to the base class method.
-
2:58
And then paste from the clipboard.
-
3:07
And finally, add the missing using directive for
-
3:10
the compactgallerymodel.modelsnamespace.
-
3:15
Let's set a break point just inside of our seed method.
-
3:19
So we can see when it's called and run the app to test our changes.
-
3:26
We didn't have our break point but that makes sense as our model didn't change.
-
3:35
Let's temporarily change our model.
-
3:39
Open the artist.cs file and add a test property.
-
3:51
Run the app again.
-
3:54
This time we hit our breakpoint in the Seed method.
-
3:57
Press F5 and then Enter to continue execution.
-
4:03
And run the app again.
-
4:07
This time we didn't hit our break point which is exactly the behavior that we want
-
4:12
as our database already contains our seed data
-
4:18
Before we wrap up this video, let's update our database initializers
-
4:22
seed method with an expanded set of test data.
-
4:25
I'll switch to Notepad, copy this code to the clipboard.
-
4:30
Switch back to Visual Studio, and replace the contents of the seed method by
-
4:34
selecting the existing code, and pasting from the clipboard.
-
4:50
If you're following along, you can find a copy of this code in the teacher's notes.
-
4:55
Now that we have the final version of our custom database initializer,
-
4:59
let's remove the test property from the artist class, and run our app.
-
5:05
And, here's the output of our final test data.
-
5:08
Let's review what we learned in this section.
-
5:10
[SOUND] We learned about One-to-Many Relationships, and
-
5:14
how to define them using navigation and foreign key entity properties.
-
5:18
Then, we learned about many-to-many relationships and
-
5:22
how to define them with or without an explicit bridge entity or table.
-
5:26
We also learned how to use data annotation attributes, EF conventions and
-
5:31
a fluent API to refine our model.
-
5:34
And lastly,
-
5:35
we created a custom database initializer which improved our database seating.
-
5:41
Our entity data model is now complete.
-
5:44
Great job working your way through the changes and updates in this section.
-
5:47
We covered a lot of ground.
-
5:50
In the next section, we'll see how to use link to write queries.
-
5:54
Let's keep going.
You need to sign up for Treehouse in order to download course files.
Sign up