How to: Add an Object to a Specific Entity Set (Entity Framework)

[This topic is pre-release documentation and is subject to change in future releases. Blank topics are included as placeholders.]

This topic uses the model and mapping files designed in the topic How to: Define a Model with Multiple Entity Sets per Type (Entity Framework).

To create an 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 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" ?>
    <add name="RegionalCustomersEntities" 
         provider connection string=&quot;
         Data Source=serverName;
         Initial Catalog=RegionalCustomersMEST;
         Integrated Security=True;
         providerName="System.Data.EntityClient" />


Add Customer and Order Entities to Entity Sets Representing Western Region

The following code creates a new instance of the Customer type and adds it to an entity sets representing the western geographical area.

To add entities of same type to separate entity sets

  1. Create an instance of the type CustomerWest and initialize the CustomerId, Name , and TotalPurchases properties of the customer.

  2. Create an instance of the type OrderWest and initialize the properties of the order.

  3. Assign the customer created in step one to the new order.

  4. Add the instances of CustomerWest and OrderWest to storage using the AddTCustomersWest and AddToOrdersWest methods. These methods are created when the data model is generated by Edmgen.exe.

  5. Create another OrderWest instance named newOrder.

  6. Create an ObjectParameter to be used in a query for the customer created in step one.

  7. Query to find the customer.

  8. Update the TotalPurchases property of the customer by adding newOrder TotalAmount property to the customers TotalPurchases.

  9. Assign the customer to newOrder.

  10. Add newOrder to storage using the AddToOrdersWest method.


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)
                using (RegionalCustomersEntities objCtx = 
                           new RegionalCustomersEntities())
                    // Create a new customer.
                    Customer customerWest = new Customer();

                    customerWest.CustomerId =
                        objCtx.CustomersWest.Count<Customer>() + 1;

                    customerWest.Name = 
                        "Customer West " + 

                    customerWest.TotalPurchases = 

                    // Create Order.
                    OrderWest orderWest = new OrderWest();
                    orderWest.OrderId = 
                        objCtx.OrdersWest.Count<OrderWest>() + 1;

                    orderWest.OrderTotal = 
                    orderWest.Tax = 
                        orderWest.OrderTotal * (decimal).07;

                    orderWest.Customer = customerWest;

                    // Add customer and order to object context.


                    // Add an order to existing customer.
                    OrderWest newOrder = new OrderWest();
                    newOrder.OrderId =
                        objCtx.OrdersWest.Count<OrderWest>() + 1;
                    newOrder.OrderTotal = (decimal)338.00;
                    newOrder.Tax = 
                               newOrder.OrderTotal * (decimal).07;

                    ObjectParameter param = 
                        new ObjectParameter("p", 3);
                    if (0 != objCtx.CustomersWest.Where(
                        "it.CustomerId = @p",
                        Customer c =
                            "it.CustomerId = @p", 

                        c.TotalPurchases =
                            c.TotalPurchases + newOrder.OrderTotal;

                        newOrder.Customer = c;
            catch (Exception e)

See Also