complex type

A complex type is a template for defining rich, structured properties on entity types or on other complex types. Each template contains the following:

  • A unique name. (Required)

    Note

    The name of a complex type cannot be the same as an entity type name within the same namespace.

  • Data in the form of one or more properties. (Optional.)

    Note

    A property of a complex type can be another complex type.

A complex type is similar to an entity type in that a complex type can carry a data payload in the form of primitive type properties or other complex types. However, there are some key differences between complex types and entity types:

  • Complex types do not have identities and therefore cannot exist independently. Complex types can only exist as properties on entity types or other complex types.

  • Complex types cannot participate in associations. Neither end of an association can be a complex type, and therefore navigation properties cannot be defined on complex types.

Example

The ADO.NET Entity Framework uses a domain-specific language (DSL) called conceptual schema definition language (CSDL) to define conceptual models. The following CSDL defines a complex type, Address, with the primitive type properties StreetAddress, City, StateOrProvince, Country, and PostalCode.

<ComplexType Name="Address" >
  <Property Type="String" Name="StreetAddress" Nullable="false" />
  <Property Type="String" Name="City" Nullable="false" />
  <Property Type="String" Name="StateOrProvince" Nullable="false" />
  <Property Type="String" Name="Country" Nullable="false" />
  <Property Type="String" Name="PostalCode" Nullable="false" />
</ComplexType>

To define the complex type Address (above) as a property on an entity type, you must declare the property type in the entity type definition. The following CSDL declares the Address property as a complex type on an entity type (Publisher):

<EntityType Name="Publisher">
      <Key>
        <PropertyRef Name="Id" />
      </Key>
      <Property Type="Int32" Name="Id" Nullable="false" />
      <Property Type="String" Name="Name" Nullable="false" />
      <Property Type="BooksModel.Address" Name="Address" Nullable="false" />
      <NavigationProperty Name="Books" Relationship="BooksModel.PublishedBy"
                          FromRole="Publisher" ToRole="Book" />
    </EntityType>

See also