HOW TO:附加相關的物件 (Entity Framework)
本主題提供的範例會示範如何將相關物件附加到物件內容。如需詳細資訊,請參閱附加物件 (Entity Framework)。重新建構使用 XML 序列化所序列化的物件圖形時會使用這個程序。
本主題的範例是根據 Adventure Works Sales Model。若要執行此範例中的程式碼,您必須已經將 AdventureWorks Sales Model 加入到專案中,並設定您的專案使用 實體架構。若要這樣做,請完成 HOW TO:手動設定 Entity Framework 專案和 HOW TO:以手動方式定義 Entity Data Model (Entity Framework) 中的程序。
範例
此範例會將中斷連結的 SalesOrderDetail 物件集合和中斷連結的 SalesOrderHeader 物件附加到物件內容,然後定義 SalesOrderHeader 物件和每個 SalesOrderDetail 物件之間的關聯性。
Private Shared Sub AttachRelatedObjects( _
ByVal currentContext As ObjectContext, _
ByVal detachedOrder As SalesOrderHeader, _
ByVal detachedItems As List(Of SalesOrderDetail))
Try
' Attach the root detachedOrder object to the supplied context.
currentContext.Attach(detachedOrder)
' Attach each detachedItem to the context, and define each relationship
' by attaching the attached SalesOrderDetail object to the EntityCollection on
' the SalesOrderDetail navigation property of the now attached detachedOrder.
For Each item As SalesOrderDetail In detachedItems
currentContext.Attach(item)
detachedOrder.SalesOrderDetail.Attach(item)
Next
Catch ex As InvalidOperationException
Console.WriteLine(ex.ToString())
End Try
End Sub
private static void AttachRelatedObjects(
ObjectContext currentContext,
SalesOrderHeader detachedOrder,
List<SalesOrderDetail> detachedItems)
{
try
{
// Attach the root detachedOrder object to the supplied context.
currentContext.Attach(detachedOrder);
// Attach each detachedItem to the context, and define each relationship
// by attaching the attached SalesOrderDetail object to the EntityCollection on
// the SalesOrderDetail navigation property of the now attached detachedOrder.
foreach (SalesOrderDetail item in detachedItems)
{
currentContext.Attach(item);
detachedOrder.SalesOrderDetail.Attach(item);
}
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.GetType().ToString() + ": " + ex.ToString());
}
}
這個範例會將中斷連結的 SalesOrderDetail 物件集合加入到中斷連結的 SalesOrderHeader 物件,然後將這個物件圖形附加到物件內容。
Private Shared Sub AttachObjectGraph( _
ByVal currentContext As ObjectContext, _
ByVal detachedOrder As SalesOrderHeader, _
ByVal detachedItems As List(Of SalesOrderDetail))
Try
' Define the relationships by adding each SalesOrderDetail
' object in the detachedItems List<SalesOrderDetail> collection to the
' EntityCollection on the SalesOrderDetail navigation property of detachedOrder.
For Each item As SalesOrderDetail In detachedItems
detachedOrder.SalesOrderDetail.Add(item)
Next
' Attach the object graph to the supplied context.
currentContext.Attach(detachedOrder)
Catch ex As InvalidOperationException
Console.WriteLine(ex.ToString())
End Try
End Sub
private static void AttachObjectGraph(
ObjectContext currentContext,
SalesOrderHeader detachedOrder,
List<SalesOrderDetail> detachedItems)
{
try
{
// Define the relationships by adding each SalesOrderDetail
// object in the detachedItems List<SalesOrderDetail> collection to the
// EntityCollection on the SalesOrderDetail navigation property of detachedOrder.
foreach (SalesOrderDetail item in detachedItems)
{
detachedOrder.SalesOrderDetail.Add(item);
}
// Attach the object graph to the supplied context.
currentContext.Attach(detachedOrder);
}
catch (InvalidOperationException ex)
{
Console.WriteLine(ex.GetType().ToString() + ": " + ex.ToString());
}
}
另請參閱
概念
序列化物件 (Entity Framework)
Web 服務和 Entity Data Model (應用程式案例)