Database Reference
In-Depth Information
3.
Insert into and query the model using code similar to the pattern shown in Listing 11-10.
Listing 11-10. Querying the Model Using the VistorySummary() Model-Defined Function
class Program
{
static void Main(string[] args)
{
RunExample();
}
static void RunExample()
{
using (var context = new EFRecipesEntities())
{
var hotel = new Hotel { Name = "Five Seasons Resort" };
var v1 = new Visitor { Name = "Alex Stevens" };
var v2 = new Visitor { Name = "Joan Hills" };
var r1 = new Reservation { Cost = 79.99M, Hotel = hotel,
ReservationDate = DateTime.Parse("2/19/2010"), Visitor = v1 };
var r2 = new Reservation { Cost = 99.99M, Hotel = hotel,
ReservationDate = DateTime.Parse("2/17/2010"), Visitor = v2 };
var r3 = new Reservation { Cost = 109.99M, Hotel = hotel,
ReservationDate = DateTime.Parse("2/18/2010"), Visitor = v1 };
var r4 = new Reservation { Cost = 89.99M, Hotel = hotel,
ReservationDate = DateTime.Parse("2/17/2010"), Visitor = v2 };
context.Hotels.Add(hotel);
context.SaveChanges();
}
using (var context = new EFRecipesEntities())
{
Console.WriteLine("Using eSQL...");
var esql = @"Select value v from
EFRecipesModel.VisitorSummary(DATETIME'2010-02-16 00:00', 7) as v";
var objectContext = (context as IObjectContextAdapter).ObjectContext;
var visitors = objectContext.CreateQuery<DbDataRecord>(esql);
foreach (var visitor in visitors)
{
Console.WriteLine("{0}, Total Reservations: {1}, Revenue: {2:C}",
visitor["Name"], visitor["TotalReservations"],
visitor["BusinessEarned"]);
}
}
using (var context = new EFRecipesEntities())
{
Console.WriteLine();
Console.WriteLine("Using LINQ...");
var visitors = from v in
Search WWH ::




Custom Search