Database Reference
In-Depth Information
Next add an App.Config classd to the project, and add the code from Listing 5-10 to it under the
ConnectionStrings section.
Listing 5-10. Connection String
<connectionStrings>
<add name="Recipe4ConnectionString"
connectionString="Data Source=.;
Initial Catalog=EFRecipes;
Integrated Security=True;
MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
In this model, we have a Club entity from which we can query information about various clubs. We can reduce
round trips to the database by directly querying the Local property of the underlying DbSet, which we use to wrap
the Club entity. The Local property exposes an observable collection of in-memory entity objects, which stays in sync
with the underlying context. Usage of the Local collection is demonstrated in Listing 5-11.
Listing 5-11. Common Usage of the Local Property for a DbSet Object
using (var context = new Recipe4Context())
{
Console.WriteLine("\nLocal Collection Behavior");
Console.WriteLine("=================");
Console.WriteLine("\nNumber of Clubs Contained in Local Collection: {0}",
context.Clubs.Local.Count);
Console.WriteLine("=================");
Console.WriteLine("\nClubs Retrieved from Context Object");
Console.WriteLine("=================");
foreach (var club in context.Clubs.Take(2))
{
Console.WriteLine("{0} is located in {1}", club.Name, club.City);
}
Console.WriteLine("\nClubs Contained in Context Local Collection");
Console.WriteLine("=================");
foreach (var club in context.Clubs.Local)
{
Console.WriteLine("{0} is located in {1}", club.Name, club.City);
}
context.Clubs.Find(desertSunId);
Console.WriteLine("\nClubs Retrieved from Context Object - Revisted");
Console.WriteLine("=================");
 
Search WWH ::




Custom Search