Complex Type Objects

Complex types are non-scalar properties of entity types that enable scalar properties to be organized within entities. Like entities, complex types consist of scalar properties or other complex type properties. Because complex types do not have keys, complex type objects cannot be managed by the Entity Framework apart from the parent object. For more information, see ComplexType Element (CSDL).

When the Entity Framework tools generate objects that represent entity types, these objects inherit from EntityObject. Likewise, objects generated for complex types inherit from ComplexObject. Both of these classes inherit from the StructuralObject base class. The scalar properties of complex type objects can be accessed like other scalar properties. For more information, see How to: Query Complex Types.

When you work with objects that represent complex types, be aware of the following points:

  • Complex type cannot contain navigation properties.

  • Complex type properties cannot be null. An InvalidOperationException occurs when SaveChanges is called and a null complex object is encountered. Like scalar properties of entities, scalar properties of complex objects can be null.

  • Complex types cannot inherit from other complex types.

  • When any property is changed anywhere in the object graph of a complex type, the property of the parent type is marked as changed and all properties in the object graph of the complex type are updated when SaveChanges is called.

  • When the object layer is generated by the Entity Data Model tools, complex objects are instantiated when the complex type property is accessed, and not when the parent object is instantiated.

When working with POCO ("plain-old" CLR objects) types that contain complex type properties, the following considerations also apply:

  • You must define the complex type as a class.

  • Changes to members of the instance of the complex type are always detected through the snapshot method. For more information, see Tracking Changes in POCO Entities.

The following topics demonstrate how to accomplish various tasks that involve complex types with the ADO.NET Entity Data Model Designer:

How to: Create and Modify Complex Types

How to: Add a Complex Type Property to an Entity

How to: Refactor Existing Properties into a Complex type Property

How to: Map Complex Type Properties to Table Columns

How to: Map a Function Import to a Complex Type

See Also

Concepts

Working with Objects