다음을 통해 공유


방법: 저장 프로시저를 사용하여 쿼리 실행(Entity Framework)

많은 응용 프로그램 개발자와 데이터베이스 관리자는 저장 프로시저를 사용하여 보안을 적용하고 예측 가능성을 제공하며 데이터베이스 내 데이터에 대한 논리를 캡슐화합니다. 저장 프로시저에 매핑된 데이터를 검색하는 응용 프로그램 코드는 FunctionImport 요소로 식별되는 함수를 사용합니다. 저장 프로시저를 EDM(엔터티 데이터 모델) 구현에 매핑하는 데 필요한 스키마 구문의 기본 요소에 대해서는 방법: 저장 프로시저로 모델 정의(Entity Framework)에서 설명합니다.

EDM은 두 가지 종류의 저장 프로시저 매핑을 지원합니다. 데이터를 업데이트하는 저장 프로시저 매핑에 대한 자세한 내용은 저장 프로시저 지원(Entity Framework)을 참조하십시오.

이 항목의 예제는 Adventure Works Sales 모델을 기반으로 합니다. 이 예제의 코드를 실행하려면 프로젝트에 AdventureWorks Sales 모델을 추가하고 Entity Framework를 사용하도록 프로젝트를 구성해야 합니다. 이렇게 하려면 방법: Entity Framework 프로젝트 수동 구성방법: 엔터티 데이터 모델 수동 정의(Entity Framework)의 절차를 수행합니다.

스키마에 다음 5가지 엔터티가 정의됩니다.

  • Address

  • Contact

  • Product

  • SalesOrderDetail

  • SalesOrderHeader

다음 단계에서는 데이터 모델의 개념 스키마에 있는 GetOrderDetailsFunctionImport에 매핑된 저장 프로시저를 실행하는 클라이언트 응용 프로그램 및 코드를 구현합니다. 이 함수는 지정된 SalesOrderHeader와 관련된 SalesOrderDetail 엔터티를 검색합니다. 이 모델의 FK_SalesOrderDetail_SalesOrderHeader_SalesOrderID 연결은 이 예제와 동일한 작업을 수행할 수 있습니다.

데이터베이스에서 저장 프로시저를 만들려면

  1. 콘솔 응용 프로그램을 만듭니다.

  2. 방법: 저장 프로시저로 모델 정의(Entity Framework) 항목에서 구현한 dll에 대한 참조를 추가합니다.

  3. System.Data.EntitySystem.Runtime.Serialization에 대한 참조를 추가합니다.

  4. 방법: 저장 프로시저로 모델 정의(Entity Framework)에서 구현한 AdventureWorksModel에 대한 전처리기 지시문을 추가합니다.

예제

저장 프로시저가 SalesOrderHeaderId에 필요한 매개 변수와 함께 사용됩니다. 개체 브라우저에서 AdventureWorksEntities 네임스페이스에 대한 메서드 형태로 표현된 다음과 같은 구문을 찾을 수 있습니다. GetOrderDetails(int). 다음 코드는 저장 프로시저를 실행하며, 여기서 반환된 결과는 foreach 루프에서 열거됩니다.

Option Explicit On
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports AdvWrksSalesModel

Module Module1
    Sub Main()
        Try
            Using db As AdvWksSalesEntities = New AdvWksSalesEntities()
                Dim soHeaderNumber As Integer = 43659
                For Each order As SalesOrderDetail _
                            In db.GetOrderDetails(soHeaderNumber)
                    Console.WriteLine("Header#: {0} " & _
                        "Order#: {1} ProductID: {2} Quantity: {3} Price: {4}", _
                        soHeaderNumber, order.SalesOrderDetailID, order.ProductID, _
                        order.OrderQty, order.UnitPrice)
                Next

            End Using

        Catch ex As System.Data.MappingException
            Console.WriteLine(ex.ToString())
        Catch ex As System.Data.CommandExecutionException
            Console.WriteLine(ex.ToString())
        End Try
    End Sub
End Module
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AdvWrksSalesModel;

namespace AdvWksSalesSProcs
{
    class Program
    {
        static void Main(string[] args)
        {
            using (AdvWksSalesEntities objCtx = 
                                new AdvWksSalesEntities())
            {
                try
                {
                    int soHeaderNumber = 43659;
                    foreach (SalesOrderDetail order in
                              objCtx.GetOrderDetails(soHeaderNumber))
                        Console.WriteLine("Header#: {0} " +
                        "Order#: {1} ProductID: {2} Quantity: {3} Price: {4}",
                        soHeaderNumber, order.SalesOrderDetailID,
                        order.ProductID,
                        order.OrderQty, order.UnitPrice);

                }
                catch (Exception e)
                {
                    Console.WriteLine(e.ToString());
                }

            }
        }
    }
}

출력은 다음과 같습니다.

Header#: 43659 Order#: 1 ProductID: 776 Quantity: 1 Price: 2024.9940
Header#: 43659 Order#: 2 ProductID: 777 Quantity: 3 Price: 2024.9940
Header#: 43659 Order#: 3 ProductID: 778 Quantity: 1 Price: 2024.9940
Header#: 43659 Order#: 4 ProductID: 771 Quantity: 1 Price: 2039.9940
Header#: 43659 Order#: 5 ProductID: 772 Quantity: 1 Price: 2039.9940
Header#: 43659 Order#: 6 ProductID: 773 Quantity: 2 Price: 2039.9940
Header#: 43659 Order#: 7 ProductID: 774 Quantity: 1 Price: 2039.9940
Header#: 43659 Order#: 8 ProductID: 714 Quantity: 3 Price: 28.8404
Header#: 43659 Order#: 9 ProductID: 716 Quantity: 1 Price: 28.8404
Header#: 43659 Order#: 10 ProductID: 709 Quantity: 6 Price: 5.7000
Header#: 43659 Order#: 11 ProductID: 712 Quantity: 2 Price: 5.1865
Header#: 43659 Order#: 12 ProductID: 711 Quantity: 4 Price: 20.1865

참고 항목

작업

방법: 저장 프로시저로 모델 정의(Entity Framework)

개념

AdventureWorks Sales 모델(EDM)
저장 프로시저 지원(Entity Framework)
ModificationFunctionMapping(EntityTypeMapping)
ModificationFunctionMapping(AssociationSetMapping)