Database Reference
In-Depth Information
13-4. Efficiently Building a Search Query
Problem
You want to write a search query using LINQ so that it is translated to more efficient SQL. Additionally, you want to
implement the Entity Framework Code-First approach.
Solution
Let's say that you have a model like the one shown in Figure 13-6 .
Figure 13-6. A simple model with a Reservation entity
To start, this example leverages the Code-First approach for Entity Framework. In Listing 13-10, we create the
entity class, Reservation.
Listing 13-10. The Reservation Entity Object
public class Reservation
{
public int ReservationId { get; set; }
public System.DateTime ResDate { get; set; }
public string Name { get; set; }
}
Next, in Listing 13-11, we create the DbContext object, which is our gateway into Entity Framework functionality
when leveraging the Code-First approach.
Listing 13-11. DbContext Object
public class Recipe5Context : DbContext
{
public Recipe4Context()
: base("Recipe4ConnectionString")
{
// disable Entity Framework Model Compatibility
Database.SetInitializer<Recipe5Context>(null);
}
 
Search WWH ::




Custom Search