Share via


如何:使用复杂类型创建和执行对象查询(实体框架)

本示例使用在主题如何:使用复杂类型定义模型(实体框架) 中定义的架构。

使用复杂类型创建项目

  1. 创建一个名为 CustomerComplexAddrClient 的控制台应用程序项目并添加对于 System.Data.EntitySystem.Runtime.Serialization 的引用。

  2. 对于从主题如何:使用复杂类型定义模型(实体框架) 中介绍的项目生成的 dll,添加针对它的引用。

  3. 将主题如何:使用复杂类型定义模型(实体框架) 中的架构添加到可执行文件所在的文件夹。

  4. 添加应用程序配置文件,如以下示例所示。

  5. 将示例中的代码复制到 Program.cs 文件中。

  6. 添加应用程序配置文件,其中具有以下所示的内容。

  7. 生成和运行项目。

<?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=CustomerWComplexAddr;
         Integrated Security=True;
         multipleactiveresultsets=true&quot;"
         providerName="System.Data.EntityClient" />
  </connectionStrings>
</configuration>

示例

此示例代码显示 CAddress 复杂类型的所有 CCustomers 属性和内部属性。对象上下文包含具有复杂属性 AddressCCustomers 的集合。Address 属性属于类型 CAddress。可以通过 CCustomer 类型的实例访问它的所有内部属性。

Option Explicit On
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports CustomerComplexAddress_VB
Imports CustomerComplexAddress_VB.CustomerComplexAddr

Module Module1
    Sub Main()
        Try
            Using objCtx As CustomerComplexAddrContext = _
                    New CustomerComplexAddrContext()
                For Each customer As CCustomer In objCtx.CCustomers
                    Console.WriteLine("Customer Id: " & _
                        "{0} {1}" & vbNewLine & "{2} {3}," & _
                        "{4} {5}" & vbNewLine & "Phone: {6}", _
                        customer.CustomerId.ToString(), _
                        customer.CompanyName, _
                        customer.Address.StreetAddress, _
                        customer.Address.City, _
                        customer.Address.Region, _
                        customer.Address.PostalCode, _
                        customer.Address.Phone)
                    Console.Write(vbNewLine)
                Next
            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 CustomerComplexAddressClient
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                using (CustomerComplexAddressContext objCtx =
                    new CustomerComplexAddressContext())
                {
                    foreach (CCustomer customer in objCtx.CCustomers)
                    {
                        Console.WriteLine("Customer Id: " +
                            "{0} {1}\r\n{2} {3}," +
                            "{4} {5}\n\rPhone: {6}", 
                            customer.CustomerId.ToString(),
                            customer.CompanyName,
                            customer.Address.StreetAddress,
                            customer.Address.City,
                            customer.Address.Region,
                            customer.Address.PostalCode,
                            customer.Address.Phone);
                    }

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

另请参见

任务

如何:使用复杂类型定义模型(实体框架)
如何:使用复杂类型添加和修改对象(实体框架)

概念

复杂类型 (EDM)