Database Reference
In-Depth Information
12.
View the Mapping Details window for the Agent. Map the columns from the Agent table
to the properties on the two complex types we've created. The mappings are shown in
Figure 2-25 .
Figure 2-25. Mapping the fields of the complex types to the columns in the Agent table
How It Works
Complex types allow you to group several properties into a single type for a property on an entity. A complex type
can contain scalar properties or other complex types, but they cannot have navigation properties or entity collections.
A complex type cannot be an entity key. Complex types are not tracked on their own in an object context.
A property whose type is a complex type cannot be null. When you work with entities with complex type
properties, you have to be mindful of this rule. Occasionally, when the value of a complex type property is unimportant
for a particular operation, you may need to create a dummy value for the property so that it has some nonnull value.
When you modify any field in complex type property, the property is marked as changed by Entity Framework,
and an update statement will be generated that will update all of the fields of the complex type property.
In Listing 2-27, we demonstrate this using the model by inserting a few agents and displaying them.
Listing 2-27. Inserting Agents and Selecting from Our Model
using (var context = new EF6RecipesContext())
{
var name1 = new Name { FirstName = "Robin", LastName = "Rosen" };
var name2 = new Name { FirstName = "Alex", LastName = "St. James" };
var address1 = new Address { AddressLine1 = "510 N. Grant",
AddressLine2 = "Apt. 8",
City = "Raytown", State = "MO",
ZIPCode = "64133" };
var address2 = new Address { AddressLine1 = "222 Baker St.",
AddressLine2 = "Apt.22B",
City = "Raytown", State = "MO",
ZIPCode = "64133" };
 
Search WWH ::




Custom Search