Database Reference
In-Depth Information
Solution
Entity Framework does not directly support filtering with the Include() method, but we can accomplish the same
thing by creating an anonymous type that includes the entity along with the filtered collection of related entities.
Let's assume that you have a model like the one in Figure 5-28 .
Figure 5-28. A model for movies and their categories
Start by adding a console application project to Visual Studio entitled Recipe13 . Be certain to reference the
Entity Framework 6 libraries. Leveraging the NuGet Package Manager does this best. Right-click on Reference, and
select Manage NuGet Packages. From the Online tab, locate and install the Entity Framework 6 package. Doing so will
download, install, and configure the Entity Framework 6 libraries in your project.
Next we create three entity objects. Create two classes: Category and Movie , and copy the code from Listing 5-33
into the classes.
Listing 5-33. Entity Classes
public class Category
{
public Category()
{
Movies = new HashSet<Movie>();
}
public int CategoryId { get; set; }
public string Name { get; set; }
public string ReleaseType { get; set; }
public virtual ICollection<Movie> Movies { get; set; }
}
public class Movie
{
public int MovieId { get; set; }
public string Name { get; set; }
public string Rating { get; set; }
public int CategoryId { get; set; }
public virtual Category Category { get; set; }
}
 
Search WWH ::




Custom Search