다음을 통해 공유


How to: Create and Execute Object Queries using Multiple Entity Sets per Type (Entity Framework)

An implementation of multiple entity sets per type (MEST) enables applications to access data of the same type from the separate partitions used in storage. The code in this section demonstrates access to a single Customer type in two separate collections returned by the query<T> provided by the object context. The code in this topic uses the data model designed in the topic How to: Define a Model with Multiple Entity Sets per Type (Entity Framework).

To configure the project and application using multiple entity sets per type

  1. Create a console application project and add references to System.Data.Entity and System.Runtime.Serialization.

  2. Add a reference to the dll built from the data model defined in the topic How to: Define a Model with Multiple Entity Sets per Type (Entity Framework).

  3. Add the schemas from the topic How to: Define a Model with Multiple Entity Sets per Type (Entity Framework) to the same folder as the executable.

  4. Add an application configuration file. The following syntax supplies a path to schema metadata and connection string to the server that hosts the data.

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <connectionStrings>
    <add name="RegionalCustomersEntities" 
         connectionString="metadata=.;
         provider=System.Data.SqlClient;
         provider connection string=&quot;
         Data Source=serverName;
         Initial Catalog=RegionalCustomersMEST;
         Integrated Security=True;
         multipleactiveresultsets=true&quot;" 
         providerName="System.Data.EntityClient" />
  </connectionStrings>

</configuration>

Example

The following example code uses multiple entity sets per type. Use this code in the project Program file.

The Customer type is accessed from both the CustomersEast query and the CustomersWest query in the following example.

The association between the customer and an order can be navigated from the OrderEast or OrderWest end of the association or from the Customer end.

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());
            }

        }
    }
}

See Also

Tasks

How to: Define a Model with Multiple Entity Sets per Type (Entity Framework)
How to: Add an Object to a Specific Entity Set (Entity Framework)