Entity Containers (EDM)

In the Entity Data Model (EDM) an EntityContainer is a logical grouping of entity sets and association sets. Because the programming model is built from the conceptual schema definition language (CSDL) schema, an EntityContainer is the specification for a namespace in the object model being defined. In the store schema definition language (SSDL) schema, the EntityContainer identifies the storage container that persists data for applications built on the model. This can be a database in a relational database management system (RDBMS) or some other technology.

The EntityContainer controls the scope of entities and relationships. All types in the EDM are defined in scope of an EntityContainer namespace. Instances of EntitySet and AssociationSet are created in scope of an EntityContainer. Similarly, instances of an EntityType or an Association are created in scope of an EntitySet or an AssociationSet.

In the XML hierarchy, the EntityContainer element is separate from the Schema element even though the EntityContainer is defined in a schema. This is important in mapping the EntityContainer to storage. In the mapping file, the fully qualified name of the EntityContainer does not include the schema namespace name.

An EntityContainer is derived from the basic EntityContainer construct provided by the EDM. The EntityContainer specifies types derived from EntitySet and AssociationSet.

An EntitySet defined in an EntityContainer can contain an EntityType defined in a separate EntityContainer.

An AssociationSet can not reference an EntitySet defined in a separate EntityContainer.

The following example shows the declarations of two entity types and an association type, and then the declaration of an EntityContainer with two entity sets and an association set.

The following schema contains the entity declarations:

<?xml version="1.0" encoding="utf-8"?>
<Schema xmlns:cg="https://schemas.microsoft.com/ado/2006/04/codegeneration"
    xmlns:edm="https://schemas.microsoft.com/ado/2006/04/edm"
    xmlns="https://schemas.microsoft.com/ado/2006/04/edm"
    Namespace="MyCompany.EntityTypes" Alias="Self">

    <EntityType Name="Order">
        <Key>
            <PropertyRef Name="OrderId" />
        </Key>
        <Property Name="OrderId" Type="String" />
        <!--Other Properties-->
    </EntityType>

    <EntityType Name="Customer">
        <Key>
            <PropertyRef Name="CustomerId" />
        </Key>
        <Property Name="CustomerId" Type="String" />
        <!--Other Properties-->
    </EntityType>

</Schema>

The following schema contains the association declarations:

<?xml version="1.0" encoding="utf-8"?>
<Schema xmlns:cg="https://schemas.microsoft.com/ado/2006/04/codegeneration"
    xmlns:edm="https://schemas.microsoft.com/ado/2006/04/edm"
    xmlns="https://schemas.microsoft.com/ado/2006/04/edm"
    Namespace="MyCompany.RelationshipTypes">

    <Using Namespace="MyCompany.EntityTypes" Alias="basicTypes"/>

    <Association Name="CustomerOrder">
        <End Type="basicTypes.Customer" Multiplicity="1" />
        <End Type="basicTypes.Order" Multiplicity="*" />
    </Association>
</Schema>

The following schema contains the entity container declaration:

<?xml version="1.0" encoding="utf-8"?>
<Schema xmlns:cg="https://schemas.microsoft.com/ado/2006/04/codegeneration"
    xmlns:edm="https://schemas.microsoft.com/ado/2006/04/edm"
    xmlns="https://schemas.microsoft.com/ado/2006/04/edm"
    Namespace="MyCompany.ContainerType">

    <EntityContainer name="ContainerOne">
        <Using Namespace="MyCompany.EntityTypes" Alias="basicTypes"/>
        <Using Namespace="MyCompany.RelationshipTypes" Alias="relnTypes"/>

        <EntitySet Name="CustomerSet" EntityType="basicTypes.Customer"/>
        <EntitySet Name="OrderSet" EntityType="basicTypes.Order"/>

        <AssociationSet Name="CustomerOrderSet" Association="relnTypes.CustomerOrder">
            <End EntitySet="CustomerSet" Role="Orders"/>
            <End EntitySet="OrderSet" Role="OrderedBy"/>
        </AssociationSet>
    </EntityContainer>
</Schema>

See Also

Concepts

Association (EDM)

Other Resources

Schemas and Mapping Specification (Entity Framework)