Export (0) Print
Expand All
Expand Minimize
0 out of 1 rated this helpful - Rate this topic

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.

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

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.