函数和函数参数(元数据)

函数是执行操作并将该操作的结果作为值返回的例程。System.Data.Metadata.Edm 命名空间提供了相关的类,以公开在 实体框架 中定义的规范函数以及在基础存储提供程序或数据库中定义的存储特定的函数。

规范函数在 实体框架 的概念性模型中定义。这些函数反映最常用的存储特定的函数。例如,在概念性模型中有一个名为 Edm.Max 的函数,在存储模型中有一个名为 SqlServer.Max 的函数。当在 Entity SQL 查询中使用规范函数时,将在存储提供程序中调用适当的函数。有关 实体框架 中的规范函数的更多信息,请参见规范函数 (Entity SQL)

可以使用 EdmFunction 类以检索有关在概念性模型中定义的规范函数的信息,以及有关在基础存储提供程序或存储模型中定义的函数的信息。

System.Data.Metadata.Edm.MetadataWorkspace.GetFunctions(System.String,System.String,System.String) 方法还使您能够检索特定函数的所有重载。

下面的代码示例演示如何通过连接获取元数据工作区,然后使用该元数据工作区以检索有关概念性模型中的 Count 规范函数的信息,以及有关存储模型中由基础存储提供程序提供的 COUNT 函数的信息。请注意,元数据工作区作为一个运行时服务组件为检索元数据提供支持。

此代码示例使用在使用 AdventureWorks 对象模型 (EDM) 中指定的应用程序配置字符串以连接到 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

另请参见

概念

元数据类型层次结构