Database Reference
In-Depth Information
public ICollection<Customer> GetCustomers(string company, string contactTitle)
{
return Database.SqlQuery<Customer>("EXEC Chapter10.GetCustomers @Company,
@ContactTitle"
, new SqlParameter("Company", company)
, new SqlParameter("ContactTitle", contactTitle))
.ToList();
}
}
3.
Follow the pattern in Listing 10-6 to use the GetCustomers stored procedure.
Listing 10-6. Querying the Model with the GetCustomers Stored Procedure via the GetCustomers()
Method
//Add customers to the database that we will query with our stored procedure.
using (var context = new EF6RecipesContext())
{
var c1 = new Customer {Name = "Robin Steele", Company = " GoShopNow.com " ,
ContactTitle="CEO"};
var c2 = new Customer {Name = "Orin Torrey", Company = " GoShopNow.com " ,
ContactTitle="Sales Manager"};
var c3 = new Customer {Name = "Robert Lancaster", Company = " GoShopNow.com " ,
ContactTitle = "Sales Manager"};
var c4 = new Customer { Name = "Julie Stevens", Company = " GoShopNow.com " ,
ContactTitle = "Sales Manager" };
context.Customers.Add(c1);
context.Customers.Add(c2);
context.Customers.Add(c3);
context.Customers.Add(c4);
context.SaveChanges();
}
using (var context = new EF6RecipesContext())
{
var allCustomers = context.GetCustomers(" GoShopNow.com " , "Sales Manager");
Console.WriteLine("Customers that are Sales Managers at GoShopNow.com " );
foreach (var c in allCustomers)
{
Console.WriteLine("Customer: {0}", c.Name);
}
}
The following is the output of the code in Listing 10-6:
Customers that are Sales Managers at GoShopNow.com
Customer: Orin Torrey
Customer: Robert Lancaster
Customer: Julie Stevens
 
 
Search WWH ::




Custom Search