Funzioni e parametri delle funzioni (metadati)

Le funzioni sono routine che eseguono un'azione e restituiscono il risultato dell'azione come valore. Lo spazio dei nomi System.Data.Metadata.Edm fornisce classi per esporre le funzioni canoniche definite in Entity Framework e le funzioni specifiche dell'archivio definite nel database o provider di archiviazione sottostante.

Le funzioni canoniche sono definite nel modello concettuale in Entity Framework. Tali funzioni riflettono le funzioni specifiche dell'archivio più comunemente utilizzate. Una funzione, ad esempio, è denominata Edm.Max nel modello concettuale e SqlServer.Max nel modello di archiviazione. Quando si utilizzano funzioni canoniche in una query Entity SQL, la funzione appropriata viene chiamata in corrispondenza del provider di archiviazione. Per ulteriori informazioni sulle funzioni canoniche in Entity Framework, vedere Funzioni canoniche (Entity SQL).

È possibile utilizzare la classe EdmFunction per recuperare informazioni sulle funzioni canoniche definite nel modello concettuale e sulle funzioni definite nel modello di archiviazione o nel provider di archiviazione sottostante.

Il metodo System.Data.Metadata.Edm.MetadataWorkspace.GetFunctions(System.String,System.String,System.String) consente inoltre di recuperare tutti gli overload di una funzione specifica.

Nell'esempio di codice seguente viene illustrato come ottenere un'area di lavoro metadati dalla connessione e quindi utilizzarla per recuperare informazioni sulle funzioni canoniche Count dal modello concettuale e sulle funzioni COUNT fornite dal provider di archiviazione sottostante nel modello di archiviazione. Si noti che l'area di lavoro metadati fornisce supporto per il recupero di metadati come componente del servizio di runtime.

Nell'esempio di codice viene utilizzata la stringa di configurazione dell'applicazione specificata in Utilizzo del modello a oggetti di AdventureWorks (EDM) per connettersi al database AdventureWorks.

using System;
using System.Data;
using System.Data.EntityClient;
using System.Collections.ObjectModel;
using System.Data.Metadata.Edm;

class GetFunctionsExample
{
  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 functions from the conceptual model.
        GetFunctionsFromModel(workspace, DataSpace.CSpace, 
                     "Count", "Edm");

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

  public static void GetFunctionsFromModel(MetadataWorkspace workspace,
        DataSpace model, string functionName, string namespaceName)
  {
     // Get a collection of EdmFunctions by using the 
     // specified function name, the namespace name and the model.
     ReadOnlyCollection<EdmFunction> functions =
           workspace.GetFunctions(
           functionName, namespaceName,
           model);

     // Iterate through the collection to get each function.
     foreach (EdmFunction function in functions)
     {
         Console.WriteLine("\nFunction Name: {0}",
                          function.FullName);

         // Check whether the current function 
         // has any parameter.
         if (function.Parameters.Count != 0)
         {
             Console.Write("Parameters: ");
             // Iterate through the collection to get 
             // each function parameter.
             foreach (FunctionParameter parameter in
                                    function.Parameters)
             {
                Console.WriteLine(
                      "\t Parameter Name: {0}",
                      parameter.Name);
              }
         }
      }
  }
}
Imports System
Imports System.Data
Imports System.Data.EntityClient
Imports System.Data.Metadata.Edm
Imports System.Collections.ObjectModel

Class GetFunctionsExample
  Public 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 functions from the conceptual model.
        GetFunctionsFromModel(workspace, DataSpace.CSpace, _
           "Count", "Edm")

        ' Get functions from the storage model.
        GetFunctionsFromModel(workspace, DataSpace.SSpace, _
            "COUNT", "SqlServer")
      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 GetFunctionsFromModel( _
    ByVal workspace As MetadataWorkspace, ByVal model As DataSpace, _
    ByVal functionName As String, ByVal namespaceName As String)

    ' Get a collection of EdmFunctions by using the 
    ' specified function name, the namespace name and the model.
    Dim functions As ReadOnlyCollection(Of EdmFunction) = _
        workspace.GetFunctions(functionName, namespaceName, model)

    ' Iterate through the collection to get each function.
    Dim functionEdm As EdmFunction
    For Each functionEdm In functions
       Console.WriteLine(ControlChars.Lf & "Function Name: {0}", _
         functionEdm.FullName)

       ' Check whether the current function 
       ' has any parameter.
       If (functionEdm.Parameters.Count <> 0) Then
          Console.Write("Parameters: ")
          ' Iterate through the collection to get 
          ' each function parameter.
          Dim parameter As FunctionParameter
          For Each parameter In functionEdm.Parameters
             Console.WriteLine(ControlChars.Tab & _
                " Parameter Name: {0}", parameter.Name)
          Next
       End If
    Next
  End Sub
End Class

Vedere anche

Concetti

Gerarchia dei tipi di metadati