Share via


Functions and Function Parameters (Metadata)

Functions are routines that perform an action and return the result of that action as a value. The System.Data.Metadata.Edm namespace provides classes to expose the canonical functions defined in the Entity Framework and also store-specific functions defined in the underlying storage provider or database.

Canonical functions are defined in the conceptual model in the Entity Framework. These functions reflect the most commonly used store-specific functions. For example, there is a function called Edm.Max in the conceptual model and SqlServer.Max in the storage model. When canonical functions are used in an Entity SQL query, the appropriate function is called at the storage provider. For more information about the canonical functions in the Entity Framework, see Canonical Functions (Entity SQL).

You can use the EdmFunction class to retrieve information about the canonical functions defined in the conceptual model and the functions defined in the underlying storage provider or storage model.

The System.Data.Metadata.Edm.MetadataWorkspace.GetFunctions(System.String,System.String,System.String) method also enables you to retrieve all the overloads of a specific function.

The following code sample demonstrates how to get a metadata workspace from the connection and then use that metadata workspace to retrieve information about the Count canonical functions from the conceptual model and the COUNT functions provided by the underlying storage provider in the storage model. Note that the metadata workspace provides support for retrieving metadata as a runtime service component.

The code sample uses the application configuration string specified in Using the AdventureWorks Object Model (EDM) to connect to the AdventureWorks database.

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

See Also

Concepts

Metadata Type Hierarchy