Database Reference
In-Depth Information
using (var context = new Recipe3Context())
{
var starCity = context.Clubs.SingleOrDefault(x => x.ClubId == starCityId);
starCity = context.Clubs.SingleOrDefault(x => x.ClubId == starCityId);
starCity = context.Clubs.Find(starCityId);
var desertSun = context.Clubs.Find(desertSunId);
var palmTree = context.Clubs.AsNoTracking().SingleOrDefault(x => x.ClubId == palmTreeId);
palmTree = context.Clubs.Find(palmTreeId);
var lonesomePintId = -999;
context.Clubs.Add(new Club {City = "Portland", Name = "Lonesome Pine", ClubId = lonesomePintId,});
var lonesomePine = context.Clubs.Find(lonesomePintId);
var nonexistentClub = context.Clubs.Find(10001);
}
How It Works
When querying against the context object, a round trip will always be made to the database to retrieve requested data,
even if that data has already been loaded into the context object in memory. When the query completes, entity objects
that do not exist in the context are added and then tracked. By default, if the entity object is already present in the
context, it is not overwritten with more recent database values.
However, the DbSet object, which wraps each of our entity objects, exposes a Find() method. Specifically, Find()
expects an argument that represents the primary key of the desired object. Find() is very efficient, as it will first search
the underlying context for the target object. If the object is not found, it then automatically queries the underlying data
store. If still not found, Find() simply returns NULL to the caller. Additionally, Find() will return entities that have
been added to the context (think, having a state of “Added”), but not yet saved to the underlying database. Fortunately,
the Find() method is available with any of three modeling approaches: Database First, Model First, or Code First.
In this example, we start by adding three new clubs to the Club entity collection. Note how we are able to
reference the newly created Id for each Club entity immediately after the call to SaveChanges() . The context will
return the Id for the new object immediately after the SaveChanges() operation completes.
We next query the Clubs entity from the DbContext object to return the StarCity Club entity. Note how we
leverage the SingleOrDefault() LINQ extension method, which returns exactly one object, or NULL, if the object
does not exist in the underlying data store. SingleOrDefault() will throw an exception if more than one object
with the search criteria is found. SingleOfDefault() is an excellent approach to querying entities by a primary key
property. If you should desire the first object when many exist, consider the FirstOrDefault() method.
If you were to run SQL Profiler tool (available in SQL Server Developer Edition or better, not in SQL Express) to
examine the underlying database activity, you would see that the SQL query shown in Figure 5-5 was generated.
Figure 5-5. SQL query returning the Star City Club
Note in Figure 5-5 how querying Clubs in the context object always results in a SQL query generated against the
underlying data store. Here we retrieve the Club with the Id of 80, materialize the data into a Club entity object, and
store it in the context. Interestingly, note how the SingleOrDefault() LINQ extension method always generates a Select
Top 2 SQL query. Interestingly, the Select Top 2 SQL query ensures that only one row is returned. If more than one row
is returned, Entity Framework will throw an exception as the SingleOrDefault() method guarantees a single result.
 
Search WWH ::




Custom Search