Database Reference
In-Depth Information
Figure 5-9. Another SQL query generated to return the Desert Sun Club
Next we add a new Club entity object to the context. We instantiate an instance of the Club entity class and populate it
with the necessary data. We assign it a temporary Id of −999. Keep in mind that we have not yet requested a SaveChanges()
operation to commit this new club, the Lonesome Pine Club, to the data store. Interestingly, when we issue a Find()
operation and pass in the argument − 999 , Entity Framework returns the newly created Lonesome Pine Club entity from
the context object. You can see in Figure 5-10 that the Find() call generated no database activity. Take note: Find() will
return a newly added entity instance from the underlying context object that has not yet been saved to the data store.
Figure 5-10. The Find() method locates the newly created, but not yet saved object in the context and returns it without
generating a query to the database
Finally, we issue a Find() query passing in an argument value that does not exist in the data store. Here we pass
an Id value of 10001. In Figure 5-11 , we see that Find() issues a SQL query to the database attempting to return a
record with an Id of 10001. Similar to the SingleOrDefault() LINQ extension method, Find() returns NULL to calling
method when it does not find the record.
Figure 5-11. The Find() method generates a SQL query and returns NULL if the record is not found in the database
5-4. Querying In-Memory Entities
Problem
You want to work with entity objects from your model, but do not want to make a round trip to the database if the
desired entity is already loaded in the in-memory context object. Additionally, you want to implement the Code-First
approach for Entity Framework 6 to manage data access.
Solution
Let's say that you have a model like the one shown in Figure 5-12 .
 
Search WWH ::




Custom Search