zt:使用复杂类型创建和执行对象查询

 使用复杂类型创建项目

  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

http://msdn.microsoft.com/zh-cn/library/bb738564(v=VS.90).aspx
原文地址:https://www.cnblogs.com/youfan/p/2009451.html