Database Reference
In-Depth Information
atm.ATMWithdrawals.Add(new ATMWithdrawal {Amount = 75.00M, Date = yesterday});
atm.ATMWithdrawals.Add(new ATMWithdrawal {Amount = 50.00M, Date= today});
context.ATMMachines.Add(atm);
context.SaveChanges();
}
using (var context = new EF6RecipesContext())
{
var forToday = context.GetWithdrawals(17, today).FirstOrDefault();
var forYesterday = context.GetWithdrawals(17, yesterday).FirstOrDefault();
var atm = context.ATMMachines.Where(o => o.ATMId == 17).FirstOrDefault();
Console.WriteLine("ATM Withdrawals for ATM at {0} at {1}",
atm.ATMId.ToString(), atm.Location);
Console.WriteLine("\t{0} Total Withdrawn = {1}",
yesterday.ToShortDateString(), forYesterday.Value.ToString("C"));
Console.WriteLine("\t{0} Total Withdrawn = {1}", today.ToShortDateString(),
forToday.Value.ToString("C"));
}
The following is the output from the code in Listing 10-10:
ATM Withdrawals for ATM at 17 at 12th and Main
5/6/2013 Total Withdrawn = $75.00
5/7/2013 Total Withdrawn = $170.00
How It Works
Notice that Entity Framework expects the stored procedure to return a collection of scalar values. In our example,
our store procedure returns just one decimal value. We use the FirstOrDefault() method to extract this scalar from
the collection.
10-4. Returning a Complex Type from a Stored Procedure
Problem
You want to use a stored procedure that returns a complex type in the model.
Solution
Let's say that you have a model with an Employee entity. Employee contains the employee's ID, name, and a complex
address type that holds the address, city, state, and ZIP code for the employee. The name of the complex type is
EmployeeAddress . The property in the Employee entity is simply Address. The Employee entity is shown in Figure 10-3 .
 
 
Search WWH ::




Custom Search