Export (0) Print
Expand All
Expand Minimize

How to: Query Objects with Multiple Entity Sets per Type

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.

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.

  3. Add the schemas from the topic How to: Define a Model with Multiple Entity Sets per Type 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

See Also

Community Additions

ADD
Show:
© 2015 Microsoft