다음을 통해 공유


함수 및 함수 매개 변수(메타데이터)

함수는 작업을 수행하고 작업의 결과를 값으로 반환하는 루틴입니다. 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

참고 항목

개념

메타데이터 형식 계층 구조