Database Reference
In-Depth Information
using (var context = new EF6RecipesContext())
{
Console.WriteLine("Artists and their albums...");
var artists = context.Artists;
foreach (var artist in artists)
{
Console.WriteLine("{0} {1}", artist.FirstName, artist.LastName);
foreach (var album in artist.Albums)
{
Console.WriteLine("\t{0}", album.AlbumName);
}
}
Console.WriteLine("\nAlbums and their artists...");
var albums = context.Albums;
foreach (var album in albums)
{
Console.WriteLine("{0}", album.AlbumName);
foreach (var artist in album.Artists)
{
Console.WriteLine("\t{0} {1}", artist.FirstName, artist.LastName);
}
}
}
The output from the code in Listing 2-3 looks like the following:
Artists and their albums...
Alan Jackson
Drive
Live at Texas Stadium
Tobby Keith
Honkytonk University
Merle Haggard
Honkytonk University
Albums and their artists...
Drive
Alan Jackson
Live at Texas Stadium
Alan Jackson
Honkytonk University
Tobby Keith
Merle Haggard
After getting an instance of our database context, we create and initialize an instance of an Artist entity type and
a couple of instances of the Album entity type. We add the albums to the artist and then add the artist to the Database
Context.
Next we create and initialize a couple of instances of the Artist entity type and an instance of the Album entity
type. Because the two artists collaborated on the album, we add the album to both artists' Albums navigation property
(which is of type EntityCollection ). Adding the album to the Database Context causes the artists to get added as well.
 
Search WWH ::




Custom Search