Condividi tramite


Procedura: eseguire query su oggetti con più set di entità per tipo (Entity Framework)

Un'implementazione di più set di entità per tipo (MEST, Multiple Entity Sets per Type) consente alle applicazioni di accedere ai dati dello stesso tipo da partizioni distinte utilizzate per l'archiviazione. Il codice in questa sezione illustra come accedere a un singolo tipo Customer in due insiemi distinti restituiti dalla query<T> fornita dal contesto dell'oggetto. Per il codice di questo argomento viene utilizzato il modello di dati progettato nell'argomento Procedura: definire un modello con più set di entità per tipo (Entity Framework).

Per configurare il progetto e l'applicazione utilizzando più set di entità per tipo

  1. Creare un progetto di applicazione console e aggiungere riferimenti a System.Data.Entity e System.Runtime.Serialization.

  2. Aggiungere un riferimento alla DLL compilata in base al modello di dati definito nell'argomento Procedura: definire un modello con più set di entità per tipo (Entity Framework).

  3. Aggiungere gli schemi inclusi nell'argomento Procedura: definire un modello con più set di entità per tipo (Entity Framework) alla stessa cartella del file eseguibile.

  4. Aggiungere un file di configurazione dell'applicazione. Nella sintassi seguente vengono forniti un percorso ai metadati dello schema e la stringa di connessione al server che ospita i dati.

<?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>

Esempio

Nel codice di esempio seguente vengono utilizzati più set di entità per tipo. Utilizzare questo codice nel file di programma del progetto.

Nell'esempio seguente viene eseguito l'accesso al tipo Customer sia dalla query CustomersEast che dalla query CustomersWest .

È possibile spostarsi nell''associazione tra il cliente e un ordine dall'entità finale OrderEast o OrderWest dell'associazione o dall'entità finale 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());
            }

        }
    }
}  

Vedere anche

Attività

Procedura: definire un modello con più set di entità per tipo (Entity Framework)