다음을 통해 공유


방법: 사용자 지정 개체에 개체 서비스 사용(Entity Framework)

사용자 지정 데이터 클래스를 사용하는 경우에도 Entity Framework 도구를 통해 생성된 데이터 클래스에서 제공되는 개체 서비스 기능을 이용할 수 있습니다. 활용할 수 없는 기능에는 다음과 같은 기능이 포함됩니다.

  • 미리 정의된 연결을 포함하여 EDM(엔터티 데이터 모델)과 관련된 ObjectContext 인스턴스화

  • 형식별 ObjectQuery 개체를 반환하는 속성

  • 개체를 특정 엔터티 집합에 추가하는 사용자 지정 메서드

개체 서비스의 기능을 모두 활용하는 가장 간단한 방법은 ObjectContext에서 자신의 프로젝트로 상속된 클래스를 생성하여 추가하는 것입니다. 이 클래스는 EDM을 기반으로 Entity Framework 도구를 통해 생성합니다.

C# 프로젝트에서 생성된 개체 코드를 사용하려면

  1. 명령 프롬프트에서 CSDL(개념 스키마 정의 언어) 파일의 위치로 이동한 후 다음 명령을 줄 바꿈 없이 실행합니다.

    %windir%\Microsoft.NET\Framework\v3.5\edmgen.exe /mode:EntityClassGeneration 
    /incsdl:.\YourModel.csdl /outobjectlayer:.\YourModel.context.cs /language:CSharp
    

    이렇게 하면 지정된 CSDL 파일을 기반으로 개체 계층이 C#으로 다시 생성됩니다.

  2. 새로 생성된 코드 파일을 열고, ObjectContext에서 상속된 클래스를 복사한 다음 사용자 지정 데이터 클래스 코드 파일에 붙여 넣습니다.

    Note참고

    데이터 클래스를 제거한 다음 나머지 클래스를 프로젝트에 추가할 수도 있습니다.

Visual Basic 프로젝트에서 생성된 개체 코드를 사용하려면

  1. 명령 프롬프트에서 CSDL 파일의 위치로 이동한 후 다음 명령을 줄 바꿈 없이 실행합니다.

    %windir%\Microsoft.NET\Framework\v3.5\edmgen.exe /mode:EntityClassGeneration 
    /incsdl:.\YourModel.csdl /outobjectlayer:.\YourModel.context.vb /language:VB
    

    이렇게 하면 지정된 CSDL 파일을 기반으로 개체 계층이 Visual Basic으로 다시 생성됩니다.

  2. 새로 생성된 코드 파일을 열고, ObjectContext에서 상속된 클래스를 복사한 다음 사용자 지정 데이터 클래스 코드 파일에 붙여 넣습니다.

    Note참고

    데이터 클래스를 제거한 다음 나머지 클래스를 프로젝트에 추가할 수도 있습니다.

예제

이 예제에서는 OrderLineItem 사용자 지정 데이터 클래스를 지원하는 생성된 개체 컨텍스트 코드를 보여 줍니다.

Option Strict Off
Option Explicit On

<Assembly: Global.System.Data.Objects.DataClasses.EdmSchemaAttribute("9f3bb474-6454-4ff1-911e-4a8be227e97c")> 

Namespace Microsoft.Samples.Edm
    '''<summary>
    '''There are no comments for SalesOrdersEntities in the schema.
    '''</summary>
    Partial Public Class SalesOrdersEntities
        Inherits Global.System.Data.Objects.ObjectContext
        '''<summary>
        '''Initializes a new SalesOrdersEntities object using the connection string found in the 'SalesOrdersEntities' section of the application configuration file.
        '''</summary>
        Public Sub New()
            MyBase.New("name=SalesOrdersEntities", "SalesOrdersEntities")
        End Sub
        '''<summary>
        '''Initialize a new SalesOrdersEntities object.
        '''</summary>
        Public Sub New(ByVal connectionString As String)
            MyBase.New(connectionString, "SalesOrdersEntities")
        End Sub
        '''<summary>
        '''Initialize a new SalesOrdersEntities object.
        '''</summary>
        Public Sub New(ByVal connection As Global.System.Data.EntityClient.EntityConnection)
            MyBase.New(connection, "SalesOrdersEntities")
        End Sub
        '''<summary>
        '''There are no comments for LineItem in the schema.
        '''</summary>
        <Global.System.ComponentModel.BrowsableAttribute(False)> _
        Public ReadOnly Property LineItem() As Global.System.Data.Objects.ObjectQuery(Of LineItem)
            Get
                If (Me._LineItem Is Nothing) Then
                    Me._LineItem = MyBase.CreateQuery(Of LineItem)("[LineItem]")
                End If
                Return Me._LineItem
            End Get
        End Property
        Private _LineItem As Global.System.Data.Objects.ObjectQuery(Of LineItem) = Nothing
        '''<summary>
        '''There are no comments for Order in the schema.
        '''</summary>
        <Global.System.ComponentModel.BrowsableAttribute(False)> _
        Public ReadOnly Property Order() As Global.System.Data.Objects.ObjectQuery(Of Order)
            Get
                If (Me._Order Is Nothing) Then
                    Me._Order = MyBase.CreateQuery(Of Order)("[Order]")
                End If
                Return Me._Order
            End Get
        End Property
        Private _Order As Global.System.Data.Objects.ObjectQuery(Of Order) = Nothing
        '''<summary>
        '''There are no comments for LineItem in the schema.
        '''</summary>
        Public Sub AddToLineItem(ByVal lineItem As LineItem)
            MyBase.AddObject("LineItem", lineItem)
        End Sub
        '''<summary>
        '''There are no comments for Order in the schema.
        '''</summary>
        Public Sub AddToOrder(ByVal order As Order)
            MyBase.AddObject("Order", order)
        End Sub
    End Class
End Namespace
[assembly: System.Data.Objects.DataClasses.EdmSchemaAttribute()]

namespace Microsoft.Samples.Edm
{

    /// <summary>
    /// There are no comments for SalesOrdersEntities in the schema.
    /// </summary>
    public partial class SalesOrdersEntities : global::System.Data.Objects.ObjectContext
    {
        /// <summary>
        /// Initializes a new SalesOrdersEntities object using the connection string found in the 'SalesOrdersEntities' section of the application configuration file.
        /// </summary>
        public SalesOrdersEntities() :
            base("name=SalesOrdersEntities", "SalesOrdersEntities")
        {
        }
        /// <summary>
        /// Initialize a new SalesOrdersEntities object.
        /// </summary>
        public SalesOrdersEntities(string connectionString) :
            base(connectionString, "SalesOrdersEntities")
        {
        }
        /// <summary>
        /// Initialize a new SalesOrdersEntities object.
        /// </summary>
        public SalesOrdersEntities(global::System.Data.EntityClient.EntityConnection connection) :
            base(connection, "SalesOrdersEntities")
        {
        }
        /// <summary>
        /// There are no comments for LineItem in the schema.
        /// </summary>
        [global::System.ComponentModel.BrowsableAttribute(false)]
        public global::System.Data.Objects.ObjectQuery<LineItem> LineItem
        {
            get
            {
                if ((this._LineItem == null))
                {
                    this._LineItem = base.CreateQuery<LineItem>("[LineItem]");
                }
                return this._LineItem;
            }
        }
        private global::System.Data.Objects.ObjectQuery<LineItem> _LineItem = null;
        /// <summary>
        /// There are no comments for Order in the schema.
        /// </summary>
        [global::System.ComponentModel.BrowsableAttribute(false)]
        public global::System.Data.Objects.ObjectQuery<Order> Order
        {
            get
            {
                if ((this._Order == null))
                {
                    this._Order = base.CreateQuery<Order>("[Order]");
                }
                return this._Order;
            }
        }
        private global::System.Data.Objects.ObjectQuery<Order> _Order = null;
        /// <summary>
        /// There are no comments for LineItem in the schema.
        /// </summary>
        public void AddToLineItem(LineItem lineItem)
        {
            base.AddObject("LineItem", lineItem);
        }
        /// <summary>
        /// There are no comments for Order in the schema.
        /// </summary>
        public void AddToOrder(Order order)
        {
            base.AddObject("Order", order);
        }
    }
}

참고 항목

작업

방법: EntityObject 및 ComplexObject 기본 클래스에서 상속(Entity Framework)
방법: 사용자 지정 데이터 클래스 인터페이스 구현(Entity Framework)

참조

EDM 생성기(EdmGen.exe)

개념

개체 사용자 지정(Entity Framework)