Database Reference
In-Depth Information
Figure 13-4. The Find() method locates the object in memory, not generating a database query
For a more detailed example of leveraging the Find() method in your application, take a look at Recipe 5-3.
13-3. Retrieving Entities for Read-Only Access
Problem
You want to retrieve some entities efficiently that you will only display and not need to update. Additionally, you want
to implement the Entity Framework Code-First approach.
Solution
A very common activity in many applications, especially websites, is to let the user browse through data. In many
cases, the user will never update the data. For these situations, you can make your code much more efficient if
you avoid the overhead of caching and change tracking from the context object. You can easily do this using the
AsNoTracking method.
Let's say that you have an application that manages appointments for doctors. Your model may look something
like the one shown in Figure 13-5 .
Figure 13-5. A model for managing doctors and their appointments
To start, this example leverages the Code-First approach for Entity Framework. In Listing 13-6, we create our
entity classes, Company, Doctor, and Appointment.
 
Search WWH ::




Custom Search