Share via


Namespace Attribute (CSDL)

The namespace name declared in conceptual schema definition language (CSDL) has several functions in the Entity Data Model (EDM). In order for the build process to connect programmable classes to the storage structures that persist data for applications, the types and containers in CSDL schema must be mapped to storage metadata. Mapping specification language (MSL) connects conceptual types to definitions in store schema definition language (SSDL) describing the storage model.

After the object model is built from the CSDL file, application code uses the namespace specified in the CSDL file to reference the classes in the DLL created by the build process. The following using directive identifies the classes in the AdventureWorksHRModel namespace.

using AdventureWorksHRModel;

The namespace declared in the CSDL schema also identifies this object model in the exe.config file that is required by EDM applications. In this example, the connection string includes the HumanResources class name. This class name is based on the name of the entity container.

For more information about entity containers, see Entity Containers (EDM).

<connectionStrings>
    <add name="HumanResources" connectionString='metadata=.;
    provider=System.Data.SqlClient; provider connection 
                                          string="server=servername;
    database=AdventureWorks; integrated security=true;
    multipleactiveresultsets=true"' providerName="System.Data.Mapping"/>
</connectionStrings>

With the previous connection string in the exe.config file, all that is necessary to instantiate the HumanResources EntityConnection for use by application code is the following statement.

HumanResources hrDb = new HumanResources();

Namespace and Mapping

The CSDL schema Schema element contains the namespace name that is used to identify the object model.

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

There is a similar line in the SSDL file.

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

Namespace names used in these declarations reflect their purposes in the data model being constructed. AdventureWorksHRModel is the conceptual model that is mapped to the target metadata namespace AdventureWorksHRTarget.

The EntityContainer elements in the conceptual schema and in the storage schema are independent of the Schema elements even though they are contained by Schema elements. In the mapping specification, the EntityContainerMapping element maps the two namespaces by references to their container objects without reference to their namespace names: edm:CdmEntityContainer="HumanResources" edm:StorageEntityContainer="HumanResources".

The following MSL heading shows the mapping between the HumanResources entity container in the conceptual schema, here referred to as CdmEntityContainer, and the storage metadata, referred to as StorageEntityContainer.

<?xml version="1.0" encoding="utf-8"?>
<Mapping edm:Space="C-S" 
    xmlns:edm="urn:schemas-microsoft-com:windows:storage:mapping:CS" 
  <EntityContainerMapping CdmEntityContainer="HumanResources" 
StorageEntityContainer="HumanResources">

Alias

The CSDL Namespace attribute has an associated Alias attribute that can be used to shorten the namespace name in the rest of the schema. The following example assigns the string Self to the Alias attribute.

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

The Alias is very useful in the rest of the schema and improves readability, as shown in the following syntax.

  <EntityContainer Name="HumanResources">
    <EntitySet Name="Department" EntityType="Self.Department" />
    <EntitySet Name="Employee" EntityType="Self.Employee" />
    <EntitySet Name="EmployeeAddress" EntityType="Self.EmployeeAddress" />

Xmlns

The previous example also contains an xmlns attribute that has an assigned URL. All CSDL schemas use this same URL.

See Also

Concepts

Storage Metadata Schema (SSDL)
Mapping Specification (MSL)
AdventureWorks Complete Model (EDM)