HOW TO:使用每個類型的多重實體來建立和執行物件查詢 (Entity Framework)
實作每個類型的多重實體 (MEST) 可讓應用程式從存放區中使用的個別分割來存取相同類型的資料。本章節的程式碼將示範如何在兩個不同的集合中存取單一 Customer 型別 (這兩個集合是由物件內容提供的 <T> 查詢所傳回)。本主題的程式碼會使用 HOW TO:使用每個類型的多重實體來定義模型 (Entity Framework) 主題所設計的資料模型。
若要使用每個類型的多重實體來設定專案和應用程式
建立主控台應用程式專案,並加入 System.Data.Entity 和 System.Runtime.Serialization 的參考。
加入從 HOW TO:使用每個類型的多重實體來定義模型 (Entity Framework) 主題所定義之資料模型建置之 dll 的參考。
將主題 HOW TO:使用每個類型的多重實體來定義模型 (Entity Framework) 中的結構描述加入至與可執行檔相同的資料夾。
加入應用程式組態檔。下列語法會提供結構描述中繼資料的路徑,以及用來連接裝載資料之伺服器的連接字串。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="RegionalCustomersEntities"
connectionString="metadata=.;
provider=System.Data.SqlClient;
provider connection string="
Data Source=serverName;
Initial Catalog=RegionalCustomersMEST;
Integrated Security=True;
multipleactiveresultsets=true""
providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
範例
下列範例程式碼會使用每個類型的多重實體,請在專案程式檔案中使用這段程式碼。
在下列範例中,Customer 型別是從 CustomersEast 查詢和 CustomersWest 查詢存取。
客戶與訂單之間的關聯可以從關聯的 OrderEast 端或 OrderWest 端或是從 Customer 端巡覽。
Option Explicit On
Option Strict On
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports RegionalCustomersModel
Module Module1
Sub Main()
Try
Using objCtx As RegionalCustomersEntities = _
New RegionalCustomersEntities()
Console.WriteLine("Number of Customers East: " & _
objCtx.CustomersEast.Count().ToString())
For Each customer As Customer In objCtx.CustomersEast
Console.WriteLine(customer.Name)
customer.OrdersEast.Load()
Dim totalOrderAmts As Decimal = 0
For Each order As OrderEast In customer.OrdersEast
Console.WriteLine( _
"Order: {0} Total: {1} Tax: {2}", _
order.OrderId, order.OrderTotal, order.Tax)
totalOrderAmts = totalOrderAmts + order.OrderTotal
Next
Console.WriteLine("Check totals: {0} <=> {1}", _
totalOrderAmts.ToString(), _
customer.TotalPurchases.ToString())
Next
Console.WriteLine("Number of Customers West: " & _
objCtx.CustomersWest.Count().ToString())
'objCtx.CustomersWest.Count(Of Customer)().ToString())
For Each customer As Customer In objCtx.CustomersWest
Console.WriteLine(customer.Name)
Dim totalOrderAmts As Decimal = 0
customer.OrdersWest.Load()
For Each order As OrderWest In customer.OrdersWest
Console.WriteLine( _
"Order: {0} Total: {1} Tax: {2}", _
order.OrderId, order.OrderTotal, _
order.Tax)
totalOrderAmts = totalOrderAmts + order.OrderTotal
Next
Console.WriteLine("Check totals: {0} <=> {1}", _
totalOrderAmts.ToString(), _
customer.TotalPurchases.ToString())
Next
' Access Customer from Order.
For Each order As OrderEast In objCtx.OrdersEast
order.CustomerReference.Load()
Console.WriteLine("{0} Order Total: {1}", _
order.Customer.Name, order.OrderTotal.ToString())
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 RegionalCustomersModel;
using System.Data.Objects;
namespace ClientRegionalCustomers
{
class Program
{
static void Main(string[] args)
{
try
{
using (RegionalCustomersEntities objCtx =
new RegionalCustomersEntities())
{
Console.WriteLine("Number of Customers East: " +
objCtx.CustomersEast.
Count<Customer>().ToString());
foreach (Customer customer in objCtx.CustomersEast)
{
Console.WriteLine(customer.Name);
customer.OrdersEast.Load();
decimal totalOrderAmts = 0;
foreach (OrderEast order in
customer.OrdersEast)
{
Console.WriteLine(
"Order: {0} Total: {1} Tax: {2}",
order.OrderId,
order.OrderTotal,
order.Tax);
totalOrderAmts =
totalOrderAmts + order.OrderTotal;
}
Console.WriteLine("Check totals: {0} <=> {1}",
totalOrderAmts.ToString(),
customer.TotalPurchases.ToString());
}
Console.WriteLine("Number of Customers West: " +
objCtx.CustomersWest.
Count<Customer>().ToString());
foreach (Customer customer in objCtx.CustomersWest)
{
Console.WriteLine(customer.Name);
decimal totalOrderAmts = 0;
customer.OrdersWest.Load();
foreach (OrderWest order in
customer.OrdersWest)
{
Console.WriteLine(
"Order: {0} Total: {1} Tax: {2}",
order.OrderId,
order.OrderTotal,
order.Tax);
totalOrderAmts = totalOrderAmts + order.OrderTotal;
}
Console.WriteLine("Check totals: {0} <=> {1}",
totalOrderAmts.ToString(),
customer.TotalPurchases.ToString());
}
// Access Customer from Order.
foreach (OrderEast order in objCtx.OrdersEast)
{
order.CustomerReference.Load();
Console.WriteLine("{0} Order Total: {1}",
order.Customer.Name,
order.OrderTotal.ToString() );
}
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
}
}
另請參閱
工作
HOW TO:使用每個類型的多重實體來定義模型 (Entity Framework)
HOW TO:將物件加入特定的實體集 (Entity Framework)