共用方式為


HOW TO:將物件加入特定的實體集 (Entity Framework)

本主題使用 HOW TO:使用每個類型的多重實體來定義模型 (Entity Framework) 主題中所設計的 Entity Data Model (EDM)。

若要使用每個類型的多重實體建立應用程式

  1. 建立主控台應用程式專案,並加入 System.Data.EntitySystem.Runtime.Serialization 的參考。

  2. 加入從 HOW TO:使用每個類型的多重實體來定義模型 (Entity Framework) 主題所定義之資料模型建置之 dll 的參考。

  3. 加入應用程式組態檔。下列語法會提供結構描述中繼資料的路徑,以及用來連接裝載資料之伺服器的連接字串。

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

將 Customer 和 Order 實體加入到代表西區的實體集

下列程式碼會建立 Customer 型別的新執行個體,並將它加入到代表西區地理位置的實體集。

若要將相同型別的實體加入到不同的實體集

  1. 建立 CustomerWest 型別的執行個體,並初始化客戶的 CustomerIdNameTotalPurchases 屬性。

  2. 建立 OrderWest 型別的執行個體,並初始化訂單的屬性。

  3. 將步驟一建立的客戶指派給新的訂單。

  4. 使用 AddTCustomersWestAddToOrdersWest 方法將 CustomerWestOrderWest 的執行個體加入至儲存區。當 Edmgen.exe 產生資料模型時,便會建立這些方法。

  5. 建立另一個名為 newOrderOrderWest 執行個體。

  6. 建立要針對步驟一建立之客戶進行查詢所使用的 ObjectParameter

  7. 進行查詢來尋找客戶。

  8. newOrderTotalAmount 屬性加入到客戶 TotalPurchases 以更新客戶的 TotalPurchases 屬性。

  9. 指派客戶給 newOrder

  10. 使用 AddToOrdersWestnewOrder 加入至儲存區。

範例

Option Explicit On
Module Module1
    Sub Main()
        Try
            Using objCtx As RegionalCustomersEntities = _
                        New RegionalCustomersEntities()

                ' Create a new customer.
                Dim customerWest As New Customer()
                customerWest.CustomerId = _
                         objCtx.CustomersWest.Count() + 1
                customerWest.Name = _
                         "CustomerWest " + _
                              customerWest.CustomerId.ToString()
                customerWest.TotalPurchases = CType(875, Decimal)

                ' Create Order.
                Dim orderWest As New OrderWest()
                orderWest.OrderId = _
                         objCtx.OrdersWest.Count() + 1
                orderWest.OrderTotal = customerWest.TotalPurchases
                orderWest.Tax = _
                         orderWest.OrderTotal * CType(0.07, Decimal)

                orderWest.Customer = customerWest

                ' Add customer and order to object context.
                objCtx.AddToCustomersWest("customerWest)
                objCtx.AddToOrdersWest(orderWest)

                objCtx.SaveChanges()

                ' Add order to existing customer.
                Dim newOrder As New OrderWest
                newOrder.OrderId = objCtx.OrdersWest.Count + 1
                newOrder.OrderTotal = CType(338.0, Decimal)
                newOrder.Tax = newOrder.OrderTotal * CType(0.07, Decimal)

                Dim param As New ObjectParameter("p", 3)
                If Not 0 = objCtx.CustomersWest. _
                      Where("it.CustomerId = @p", param).Count() Then
                    Dim c As Customer = _
                        objCtx.CustomersWest. _
                        Where("it.CustomerId = @p", param).FirstOrDefault()

                    c.TotalPurchases = c.TotalPurchases + newOrder.OrderTotal

                    newOrder.Customer = c
                    objCtx.SaveChanges()

                End If

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

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

                    customerWest.Name = 
                        "Customer West " + 
                        customerWest.CustomerId.ToString();

                    customerWest.TotalPurchases = 
                        (decimal)875.00;

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

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

                    orderWest.Customer = customerWest;

                    // Add customer and order to object context.
                    objCtx.AddToCustomersWest(customerWest);
                    objCtx.AddToOrdersWest(orderWest);

                    objCtx.SaveChanges();

                    // 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",
                         param).Count<Customer>())
                    {
                        Customer c =
                            objCtx.CustomersWest.Where(
                            "it.CustomerId = @p", 
                            param).First<Customer>();

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

                        newOrder.Customer = c;
                        
                        objCtx.SaveChanges();
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}

另請參閱

工作

HOW TO:使用每個類型的多重實體來定義模型 (Entity Framework)
HOW TO:使用每個類型的多重實體來建立和執行物件查詢 (Entity Framework)