다음을 통해 공유


방법: 복합 형식으로 개체 추가 및 수정(Entity Framework)

이 항목에서는 방법: 복합 형식으로 모델 정의(Entity Framework)에서 디자인한 엔터티 데이터 모델을 사용합니다.

응용 프로그램을 만들고 복합 형식을 저장소에 추가하려면

  1. CustomerComplexAddrClient라는 콘솔 응용 프로그램 프로젝트를 만듭니다.

  2. System.Data.Entity 및 System.Runtime.Serialization 네임스페이스에 대한 참조를 추가합니다.

  3. 방법: 복합 형식으로 모델 정의(Entity Framework) 항목에서 설명한 프로젝트에서 빌드한 dll에 대한 참조를 추가합니다.

  4. 방법: 복합 형식으로 모델 정의(Entity Framework) 항목의 스키마를 실행 파일과 같은 폴더에 추가합니다.

  5. 아래와 같이 응용 프로그램 구성 파일을 만듭니다.

  6. 예제 코드를 Program.cs 파일에 복사합니다.

  7. 프로젝트를 빌드하고 실행합니다.

// The following syntax is used in the App.config file.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="CustomerComplexAddressContext" 
         connectionString="metadata=.;
         provider=System.Data.SqlClient;
         provider connection string=&quot;
         Data Source=serverName;
         Initial Catalog=CustomerWComplexAddress;
         Integrated Security=True;
         multipleactiveresultsets=true&quot;"
         providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

예제

제시된 예제 코드에서는 복합 형식의 CCustomerCAddress 엔터티를 만듭니다. CCustomerCAddress의 속성이 초기화되며, CAddressCCustomerAddress 속성에 할당됩니다. CCustomerCAddress 모두 저장소에 추가되고 변경 내용이 저장됩니다.

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


Module Module1

    Sub Main()
        Try
            Using objCtx As CustomerComplexAddressContext = _
                              New CustomerComplexAddressContext()

                Dim newCustomer12 As CCustomer = New CCustomer()
                newCustomer12.CustomerId = 12
                newCustomer12.ContactTitle = "Title 12"
                newCustomer12.ContactName = "Contact 12"
                newCustomer12.CompanyName = "Company 12"

                Dim newAddress12 As CAddress = New CAddress()
                newAddress12.StreetAddress = "1121 St"
                newAddress12.City = "Redmond"
                newAddress12.Region = "WA"
                newAddress12.Country = "USA"
                newAddress12.PostalCode = "97612"
                newAddress12.Phone = "2344567812"
                newAddress12.Fax = "3451238712"

                newCustomer12.Address = newAddress12

                objCtx.AddToCCustomers(newCustomer12)
                objCtx.SaveChanges()

            End Using
        Catch ex As Exception
            Console.WriteLine(ex.ToString())
        End Try

    End Sub

End Module
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CustomerComplexAddress;

namespace CustomerComplexAddrClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (CustomerComplexAddressContext objCtx =
                    new CustomerComplexAddressContext())
                {
                    CCustomer newCustomer10 = new CCustomer();
                    newCustomer10.CustomerId = 10;
                    newCustomer10.ContactTitle = "Title 10";
                    newCustomer10.ContactName = "Contact 10";
                    newCustomer10.CompanyName = "Company 10";

                    CAddress newAddress10 = new CAddress();
                    newAddress10.StreetAddress = "1001 St";
                    newAddress10.City = "Redmond";
                    newAddress10.Region = "WA";
                    newAddress10.Country = "USA";
                    newAddress10.PostalCode = "97600";
                    newAddress10.Phone = "2344567890";
                    newAddress10.Fax = "3451238700";

                    newCustomer10.Address = newAddress10;

                    objCtx.AddToCCustomers(newCustomer10);
                    objCtx.SaveChanges();
                    
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}

참고 항목

작업

방법: 복합 형식으로 모델 정의(Entity Framework)
방법: 복합 형식으로 개체 쿼리 만들기 및 실행(Entity Framework)

개념

복합 형식(EDM)