Database Reference
In-Depth Information
Next create a class entitled Recipe13Context and add the code from Listing 5-34 to it, ensuring the class derives
from the Entity Framework DbContext class.
Listing 5-34. Context Class
public class Recipe13Context : DbContext
{
public Recipe13Context()
: base("Recipe13ConnectionString")
{
// Disable Entity Framework Model Compatibility
Database.SetInitializer<Recipe13Context>(null);
}
public DbSet<Category> Categories { get; set; }
public DbSet<Movie> Movies { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Category>().ToTable("Chapter5.Category");
modelBuilder.Entity<Movie>().ToTable("Chapter5.Movie");
}
}
Next add an App.Config class to the project, and add the code from Listing 5-35 to it under the
ConnectionStrings section.
Listing 5-35. Connection String
<connectionStrings>
<add name="Recipe13ConnectionString"
connectionString="Data Source=.;
Initial Catalog=EFRecipes;
Integrated Security=True;
MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
To eagerly load and filter both the categories and their associated movies, follow the pattern in Listing 5-36.
Listing 5-36. Filtering an Eagerly Loaded Entity Collection
using (var context = new EFRecipesEntities())
{
var cat1 = new Category { Name = "Science Fiction", ReleaseType = "DVD" };
var cat2 = new Category { Name = "Thriller", ReleaseType = "Blu-Ray" };
new Movie { Name = "Return to the Moon", Category = cat1, Rating = "PG-13" };
new Movie { Name = "Street Smarts", Category = cat2, Rating = "PG-13" };
new Movie { Name = "Alien Revenge", Category = cat1, Rating = "R" };
new Movie { Name = "Saturday Nights", Category = cat1, Rating = "PG-13" };
context.Categories.AddObject(cat1);
context.Categories.AddObject(cat2);
context.SaveChanges();
}
Search WWH ::




Custom Search