如何:使用每种类型多个实体集查询对象(实体框架)
每种类型多个实体集 (MEST) 的实现使应用程序可以从在存储中使用的单独分区中访问相同类型的数据。 本节的代码演示如何在由对象上下文提供的查询 <T> 返回的两个单独集合中访问单个 Customer 类型。 本主题中的代码使用在主题如何:通过每种类型多个实体集定义模型(实体框架) 中设计的数据模型。
使用每种类型多个实体集配置项目和应用程序
创建一个控制台应用程序项目,并添加对 System.Data.Entity 和 System.Runtime.Serialization 的引用。
对于从在主题如何:通过每种类型多个实体集定义模型(实体框架) 中定义的数据模型生成的 dll,添加对它的引用。
将主题如何:通过每种类型多个实体集定义模型(实体框架) 中的架构添加到与可执行文件所在的文件夹。
添加应用程序配置文件。 以下语法将指向架构元数据和连接字符串的路径提供给承载数据的服务器。
<?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>
示例
以下代码示例使用每种类型多个实体集。 请在项目程序文件中使用此代码。
在以下示例中,可以从 CustomersEast 查询和 CustomersWest 查询访问 Customer 类型。
对于客户与订单之间的关联,可以从该关联的 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());
}
}
}
}