Database Reference
In-Depth Information
Address = new EmployeeAddress {
Address = "3920 South Beach",
City = "Fort Worth", State = "TX",
ZIP = "76102" } };
var emp3 = new Employee { Name = "Steven Chue",
Address = new EmployeeAddress {
Address = "129 Barker",
City = "Euless", State = "TX",
ZIP = "76092" } };
var emp4 = new Employee { Name = "Karen Stevens",
Address = new EmployeeAddress {
Address = "108 W. Parker",
City = "Fort Worth", State = "TX",
ZIP = "76102" } };
context.Employees.Add(emp1);
context.Employees.Add(emp2);
context.Employees.Add(emp3);
context.Employees.Add(emp4);
context.SaveChanges();
}
using (var context = new EF6RecipesContext())
{
Console.WriteLine("Employee addresses in Fort Worth, TX");
foreach (var address in context.GetEmployeeAddresses("Fort Worth"))
{
Console.WriteLine("{0}, {1}, {2}, {3}", address.Address,
address.City, address.State, address.ZIP);
}
}
The following is the output of the code in Listing 10-12:
Employee addresses in Fort Worth, TX
100 E. Main, Fort Worth, TX, 76106
3920 South Beach, Fort Worth, TX, 76102
108 W. Parker, Fort Worth, TX, 76102
How It Works
Complex types offer a convenient way to refactor repeated groups of properties into a single type that can be
reused across many entities. In this recipe, we created a stored procedure that returned the address information for
employees in a given city. In the model, we mapped these returned columns to the fields of the EmployeeAddress
complex type. The GetEmployeeAdresses() method is defined by the Function Import Wizard to return a collection of
instances of the EmployeeAddress type.
Complex types are often used to hold arbitrarily shaped data returned from a stored procedure. The data is not
required to map to any entity in the model. Because complex types are not tracked by the object context, they are both
a lightweight and efficient alternative to handling shaped data in the model.
 
Search WWH ::




Custom Search