本主题使用如何:使用复杂类型定义模型(实体框架) 中设计的实体数据模型。
创建应用程序并向存储中添加复杂类型
- 创建名为 CustomerComplexAddrClient 的控制台应用程序项目。 
- 添加对 System.Data.Entity 和 System.Runtime.Serialization 命名空间的引用。 
- 对于从主题如何:使用复杂类型定义模型(实体框架) 中介绍的项目生成的 dll,添加针对它的引用。 
- 将主题如何:使用复杂类型定义模型(实体框架) 中的架构添加到可执行文件所在的文件夹。 
- 创建如下所示的应用程序配置文件。 
- 将示例代码复制到 Program.cs 文件中。 
- 生成和运行项目。 
// 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="
         Data Source=serverName;
         Initial Catalog=CustomerWComplexAddress;
         Integrated Security=True;
         multipleactiveresultsets=true""
         providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>
示例
所示的示例代码创建了 CCustomer 的一个实体和一个复杂类型的 CAddress。对 CCustomer 和 CAddress 的属性进行了初始化。CAddress 指定为 CCustomer 的 Address 属性。CCustomer 和 CAddress 都被添加到存储中并保存了更改。
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());
            }
        }
    }
}
另请参见
任务
如何:使用复杂类型定义模型(实体框架)
如何:使用复杂类型创建和执行对象查询(实体框架)