Database Reference
In-Depth Information
Listing 10-17. Using the GetAllMedia Stored Procedure via the GetAllMedia() Method
Using (var context = new EF6RecipesContext())
{
context.MediaSet.Add(new Magazine { Title = "Field and Stream",
PublicationDate = DateTime.Parse("6/12/1945") });
context.MediaSet.Add(new Magazine { Title = "National Geographic",
PublicationDate = DateTime.Parse("7/15/1976") });
context.MediaSet.Add(new DVD { Title = "Harmony Road",
PlayTime = "2 hours, 30 minutes" });
context.SaveChanges();
}
using (var context = new EF6RecipesContext())
{
var allMedia = context.GetAllMedia();
Console.WriteLine("All Media");
Console.WriteLine("=========");
foreach (var m in allMedia)
{
if (m is Magazine)
Console.WriteLine("{0} Published: {1}", m.Title,
((Magazine)m).PublicationDate.ToShortDateString());
else if (m is DVD)
Console.WriteLine("{0} Play Time: {1}", m.Title, ((DVD)m).PlayTime);
}
}
The following is the output of the code in Listing 10-17:
All Media
=========
Field and Stream Published: 6/12/1945
National Geographic Published: 7/15/1976
Harmony Road Play Time: 2 hours, 30 minutes
How It Works
The two key parts to the solution are the discriminator column injected into the result set by the stored procedure and
the conditional mapping of the results to the Magazine and DVD entities.
Note
the discriminator column is a metadata column that specifies the type of object represented by the database
record.
 
 
Search WWH ::




Custom Search