Database Reference
In-Depth Information
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Reservation>().ToTable("Chapter13.Reservation");
}
public DbSet<Reservation> Reservations { get; set; }
}
Next add an App.Config class to the project, and also add to it the code from Listing 13-12, under the
ConnectionStrings section.
Listing 13-12.
Connection String
<connectionStrings>
<add name="Recipe4ConnectionString"
connectionString="Data Source=.;
Initial Catalog=EFRecipes;
Integrated Security=True;
MultipleActiveResultSets=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
You want to write a search query using LINQ to find reservations for a particular person, or reservations on a
given date, or both. You might use the
let
keyword as we did in the
first
query in Listing 13-13 to make the LINQ
expression fairly clean and easy to read. However, the
let
keyword is translated to more complex and often less
efficient SQL. Instead of using the
let
keyword, consider explicitly creating two conditions in the
where
clause, as we
did in the
second
query in Listing 13-13.
Listing 13-13.
Using Both the
let
Keyword and Explicit Conditions in the Query
using (var context = new Recipe4Context())
{
context.Reservations.Add(new Reservation
{Name = "James Jordan", ResDate = DateTime.Parse("4/18/10")});
context.Reservations.Add(new Reservation {Name = "Katie Marlowe",
ResDate = DateTime.Parse("3/22/10")});
context.Reservations.Add(new Reservation {Name = "Roger Smith",
ResDate = DateTime.Parse("4/18/10")});
context.Reservations.Add(new Reservation {Name = "James Jordan",
ResDate = DateTime.Parse("5/12/10")});
context.Reservations.Add(new Reservation {Name = "James Jordan",
ResDate = DateTime.Parse("6/22/10")});
context.SaveChanges();
}
using (var context = new Recipe4Context())
{
DateTime? searchDate = null;
var searchName = "James Jordan";