Types (Métadonnées)

Les types sont les constructions de niveau supérieur qui servent de base à un modèle EDM (Modèle de données d'entité). ADO.NET fournit l'espace de noms System.Data.Metadata.Edm, lequel contient un ensemble de types qui représentent les concepts définis dans le modèle EDM d'Entity Framework. Pour plus d'informations sur les modèles utilisés dans Entity Framework, voir Modélisation des données dans Entity Framework et Vue d'ensemble de la classe MetadataWorkspace.

Comme cela est décrit dans la rubrique Vue d'ensemble de la hiérarchie des types de métadonnées, un objet EdmType est une classe de base pour les classes qui représentent les types du modèle EDM. Les types de niveau supérieur tels que SimpleType, StructuralType, CollectionType et RefType, dérivent du type EdmType.

  • L'objet SimpleType décrit les types primitifs. Pour plus d'informations sur les types simples, voir Types simples (Métadonnées).

  • L'objet StructuralType est un type de base pour tous les types de la hiérarchie des types de métadonnées qui disposent de membres. Pour plus d'informations sur les types structurels, voir Types structurels (Métadonnées).

  • L'objet CollectionType décrit une collection d'instances d'un type spécifique.

  • L'objet RefType contient l'adresse d'une entité pour les opérations qui utilise l'entité.

La rubrique Types de modèles EDM fournit également des informations détaillées sur la façon dont les types sont utilisés dans le modèle EDM.

Les exemples de code suivants montrent comment obtenir un espace de travail des métadonnées à partir de la connexion, puis utiliser cet espace pour récupérer les informations relatives à un type spécifique et à tous les autres types du modèle spécifié. Les exemples de code utilisent un objet CSpace et un SSpace pour spécifier le modèle. CSpace représente le nom par défaut du modèle conceptuel. SSpace représente le nom par défaut du modèle de stockage. Notez que l'espace de travail des métadonnées est un composant de service du runtime qui fournit une prise en charge de la récupération des métadonnées.

Les exemples de code utilisent le modèle AdventureWorks fourni dans la rubrique Modèle complet AdventureWorks (EDM). Pour obtenir un exemple du fichier de configuration d'application, voir Utilisation du modèle objet AdventureWorks (EDM).

// The first example:
using System;
using System.Data;
using System.Data.EntityClient;
using System.Collections.ObjectModel;
using System.Data.Metadata.Edm;

class GetTypeExample
{
  static void Main()
  {
    try
    {
      // Establish a connection to the underlying data provider by 
      // using the connection string specified in the config file.
      using (EntityConnection connection = 
         new EntityConnection("Name=AdventureWorksEntities"))
      {
        // Open the connection.
        connection.Open();

        // Access the metadata workspace.
        MetadataWorkspace workspace = 
              connection.GetMetadataWorkspace();

        // Get an EntityType object by using the specified type name, 
        // the namespace name, and the model. 
        EdmType departmentType1 = workspace.GetType(
              "Department", "AdventureWorksModel", DataSpace.CSpace);

        Console.WriteLine(
           "Type found in the conceptual model Name: {0}, {1} ",
           departmentType1.Name, 
           departmentType1.NamespaceName);

        // Get an EntityType object by using the specified type name, 
        // the namespace name, and the model. 
        EdmType departmentType2 = workspace.GetType( 
           "Department", "AdventureWorksModel.Store",
           DataSpace.SSpace);

        Console.WriteLine(
          "Type found in the storage model Name: {0}, {1} ",
          departmentType2.Name,
          departmentType2.NamespaceName);
      }
    }
    catch (MetadataException exceptionMetadata)
    {
      Console.WriteLine("MetadataException: {0}", 
                       exceptionMetadata.Message);
    }
    catch (System.Data.MappingException exceptionMapping)
    {
      Console.WriteLine("MappingException: {0}",
                       exceptionMapping.Message);
    }
  }
}
// The second example:
using System;
using System.Data;
using System.Data.EntityClient;
using System.Data.Metadata.Edm;
using System.Collections.ObjectModel;

class GetTypesExample
{
  static void Main()
  {
    try
    {
       // Establish a connection to the underlying data provider by 
       // using the connection string specified in the config file.
       using (EntityConnection connection = 
           new EntityConnection("Name=AdventureWorksEntities"))
       {
          // Open the connection.
          connection.Open();

          // Access the metadata workspace.
          MetadataWorkspace workspace = 
               connection.GetMetadataWorkspace();

          // Get types from the conceptual model.
          GetTypesFromModel(workspace, DataSpace.CSpace);

          // Get types from the storage model.
          GetTypesFromModel(workspace, DataSpace.SSpace);
       }
    }
    catch (MetadataException exceptionMetadata)
    {
      Console.WriteLine("MetadataException: {0}", 
                       exceptionMetadata.Message);
    }
    catch (System.Data.MappingException exceptionMapping)
    {
       Console.WriteLine("MappingException: {0}",
                        exceptionMapping.Message);
    }
  }

  public static void GetTypesFromModel(
    MetadataWorkspace workspace, DataSpace model)
  {
    // Get a collection of the EdmTypes. 
    // An EdmType class is the base class for the classes 
    // that represent types in the Entity Data Model (EDM).
    ReadOnlyCollection<EdmType> types =
          workspace.GetItems<EdmType>(model);

    // Iterate through the collection to get each edm type, 
    // specifically each entity type.
    foreach (EdmType item in types)
    {
       EntityType entityType = item as EntityType;
       if (entityType != null)
       {
          Console.WriteLine("Type: {0}, Type in Model: {1} ",
                item.GetType().FullName, item.FullName);
       }
     }
  }
}
' The first example:
Imports System
Imports System.Data
Imports System.Data.EntityClient
Imports System.Data.Metadata.Edm

Class GetTypeExample
   Shared Sub Main()
     Try
       ' Establish a connection to the underlying data provider by 
       ' using the connection string specified in the config file.
       Using connection As EntityConnection = _
          New EntityConnection("Name=AdventureWorksEntities")

          ' Open the conection.
          connection.Open()

          ' Access the metadata workspace.
           Dim workspace As MetadataWorkspace = _
             connection.GetMetadataWorkspace

           ' Get an EntityType object by using the specified type name, 
           ' the namespace name, and the model. 
           Dim departmentType1 As EdmType = _
              workspace.GetType("Department", "AdventureWorksModel", _
              DataSpace.CSpace)
           Console.WriteLine( _
           "Type found in the conceptual model Name: {0}, {1} ", _
           departmentType1.Name, _
           departmentType1.NamespaceName)

           ' Get an EntityType object by using the specified type name, 
           ' the namespace name, and the model. 
           Dim departmentType2 As EdmType = _
               workspace.GetType("Department", _
               "AdventureWorksModel.Store", _
               DataSpace.SSpace)
           Console.WriteLine( _
              "Type found in the storage model Name: {0}, {1} ", _
              departmentType2.Name, _
              departmentType2.NamespaceName)
        End Using
     Catch exceptionMetadata As MetadataException
        Console.WriteLine("MetadataException: {0}", _
          exceptionMetadata.Message)
     Catch exceptionMapping As MappingException
        Console.WriteLine("MappingException: {0}", _
          exceptionMapping.Message)
     End Try
   End Sub
End Class
' The second example:
Imports System
Imports System.Collections.ObjectModel
Imports System.Data
Imports System.Data.EntityClient
Imports System.Data.Metadata.Edm

Class GetTypesExample
   Shared Sub Main()
     Try
       ' Establish a connection to the underlying data provider by 
       ' using the connection string specified in the config file.
       Using connection As EntityConnection = _
          New EntityConnection("Name=AdventureWorksEntities")

         ' Open the conection.
         connection.Open()

         ' Access the metadata workspace.
         Dim workspace As MetadataWorkspace = _
           connection.GetMetadataWorkspace

         ' Get types from the conceptual model.
         GetTypesFromModel(workspace, DataSpace.CSpace)

         ' Get types from the storage model.
         GetTypesFromModel(workspace, DataSpace.SSpace)
       End Using
     Catch exceptionMetadata As MetadataException
        Console.WriteLine("MetadataException: {0}", _
          exceptionMetadata.Message)
     Catch exceptionMapping As MappingException
        Console.WriteLine("MappingException: {0}", _
          exceptionMapping.Message)
     End Try
   End Sub

  Public Shared Sub GetTypesFromModel( _
     ByVal workspace As MetadataWorkspace, ByVal model As DataSpace)
     ' Get a collection of the EdmTypes. 
     ' An EdmType class is the base class for the classes 
     ' that represent types in the Entity Data Model (EDM).
     Dim types As ReadOnlyCollection(Of EdmType) = _
         workspace.GetItems(Of EdmType)(model)
     Dim item As EdmType

     ' Iterate through the collection to get each edm type, 
     ' specifically each entity type.
     For Each item In types
       Dim entityType As EntityType = TryCast(item, EntityType)
       If (Not entityType Is Nothing) Then
         Console.WriteLine("Type: {0}, Type in Model: {1} ", _
             item.GetType.FullName, item.FullName)
        End If
     Next
   End Sub
End Class

Dans cette section

Voir aussi

Concepts

Hiérarchie des types de métadonnées
Vue d'ensemble de la hiérarchie des types de métadonnées
Types de modèles EDM