HOW TO:執行可傳回複雜型別的查詢 (EntityClient)
本主題顯示如何執行會傳回內含複雜型別屬性之實體類型物件的 Entity SQL 查詢。
執行這個範例中的程式碼
將 AdventureWorks 銷售模型 加入至專案中,並將專案設定為使用 Entity Framework 。 如需詳細資訊,請參閱 HOW TO:使用實體資料模型精靈 (Entity Framework)。
在應用程式的字碼頁中加入下列 using 陳述式 (在 Visual Basic 中為 Imports):
Imports System Imports System.Collections.Generic Imports System.Collections Imports System.Data.Common Imports System.Data Imports System.IO Imports System.Data.SqlClient Imports System.Data.EntityClient Imports System.Data.Metadata.Edm
using System; using System.Collections.Generic; using System.Collections; using System.Data.Common; using System.Data; using System.IO; using System.Data.SqlClient; using System.Data.EntityClient; using System.Data.Metadata.Edm;
您可以在實體設計工具的 [模型瀏覽器] 視窗中按兩下 .edmx 檔顯示模型。 在實體設計工具介面上,選取 Contact 實體類型的 Email 和 Phone 屬性,然後以滑鼠右鍵按一下並選取 [重構至新的複雜型別]。
含有已選取之 Email 和 Phone 屬性的新複雜型別隨即會加入到 [模型瀏覽器]。 複雜型別已有預設名稱:請在 [屬性] 視窗中將型別重新命名為 EmailPhone。 也會將新的 ComplexProperty 屬性加入至 Contact 實體類型。 將屬性重新命名為 EmailPhoneComplexType.
如需使用實體資料模型精靈來建立及修改複雜型別的詳細資訊,請參閱 How to: Refactor Existing Properties into a Complex Type Property (Entity Data Model Tools) 和 How to: Create and Modify Complex Types。
範例
下列範例所執行的查詢,會傳回 Contact 物件的集合並顯示 Contact 物件的兩個屬性:ContactID 和 EmailPhoneComplexType 複雜型別的值。
Using conn As New EntityConnection("name=AdventureWorksEntities")
conn.Open()
Dim esqlQuery As String = "SELECT VALUE contacts FROM" & _
" AdventureWorksEntities.Contacts AS contacts WHERE contacts.ContactID == @id"
' Create an EntityCommand.
Using cmd As EntityCommand = conn.CreateCommand()
cmd.CommandText = esqlQuery
Dim param As New EntityParameter()
param.ParameterName = "id"
param.Value = 3
cmd.Parameters.Add(param)
' Execute the command.
Using rdr As EntityDataReader = cmd.ExecuteReader(CommandBehavior.SequentialAccess)
' The result returned by this query contains
' Address complex Types.
While rdr.Read()
' Display CustomerID
Console.WriteLine("Contact ID: {0}", rdr("ContactID"))
' Display Address information.
Dim nestedRecord As DbDataRecord = TryCast(rdr("EmailPhoneComplexProperty"), DbDataRecord)
Console.WriteLine("Email and Phone Info:")
For i As Integer = 0 To nestedRecord.FieldCount - 1
Console.WriteLine((" " & nestedRecord.GetName(i) & ": ") + nestedRecord.GetValue(i))
Next
End While
End Using
End Using
conn.Close()
End Using
using (EntityConnection conn =
new EntityConnection("name=AdventureWorksEntities"))
{
conn.Open();
string esqlQuery = @"SELECT VALUE contacts FROM
AdventureWorksEntities.Contacts AS contacts
WHERE contacts.ContactID == @id";
// Create an EntityCommand.
using (EntityCommand cmd = conn.CreateCommand())
{
cmd.CommandText = esqlQuery;
EntityParameter param = new EntityParameter();
param.ParameterName = "id";
param.Value = 3;
cmd.Parameters.Add(param);
// Execute the command.
using (EntityDataReader rdr =
cmd.ExecuteReader(CommandBehavior.SequentialAccess))
{
// The result returned by this query contains
// Address complex Types.
while (rdr.Read())
{
// Display CustomerID
Console.WriteLine("Contact ID: {0}",
rdr["ContactID"]);
// Display Address information.
DbDataRecord nestedRecord =
rdr["EmailPhoneComplexProperty"] as DbDataRecord;
Console.WriteLine("Email and Phone Info:");
for (int i = 0; i < nestedRecord.FieldCount; i++)
{
Console.WriteLine(" " + nestedRecord.GetName(i) +
": " + nestedRecord.GetValue(i));
}
}
}
}
conn.Close();
}