Delen via


ComplexType Element (CSDL)

A ComplexType element defines a data structure composed of EdmSimpleType properties or other complex types. For more information about the EdmSimpleType, see Conceptual Model Types (CSDL). A complex type can be a property of an entity type or another complex type. A complex type is similar to an entity type in that a complex type defines data. However, there are some key differences between complex types and entity types:

  • Complex types do not have identities (or keys) and therefore cannot exist independently. Complex types can only exist as properties of 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 for complex types.

  • A complex type property cannot have a null value, though the scalar properties of a complex type may each be set to null.

A ComplexType element can have the following child elements (in the order listed):

The table below describes the attributes that can be applied to the ComplexType element.

Attribute Name Is Required Value

Name

Yes

The name of the complex type. The name of a complex type cannot be the same as the name of another complex type, entity type, or association that is within the scope of the model.

BaseType

No

The name of another complex type that is the base type of the complex type that is being defined.

Cc716799.note(en-us,VS.100).gifNote:
This attribute is not applicable in the CSDL version specified by the namespace https://schemas.microsoft.com/ado/2006/04/edm. Inheritance for complex types is not supported in that version.

Abstract

No

True or False (the default value) depending on whether the complex type is an abstract type.

Cc716799.note(en-us,VS.100).gifNote:
This attribute is not applicable in the CSDL version specified by the namespace https://schemas.microsoft.com/ado/2006/04/edm. Complex types in that version cannot be abstract types.

Note

Any number of annotation attributes (custom XML attributes) may be applied to the ComplexType element. However, custom attributes may not belong to any XML namespace that is reserved for CSDL. The fully-qualified names for any two custom attributes cannot be the same.

Example

The following example shows a complex type, Address, with the EdmSimpleType 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 of an entity type, you must declare the property type in the entity type definition. The following example shows 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

Concepts

Entity Framework Overview
CSDL Specification
Schema Element (CSDL)
EntityType Element (CSDL)
Complex Type Objects

Other Resources

CSDL, SSDL, and MSL Specifications
ADO.NET Entity Data Model Tools
complex type (Entity Data Model)