EntityCollection<TEntity>.Attach 方法

定義

在物件內容中,定義兩個所附加物件之間的關聯性。Defines a relationship between two attached objects in an object context.

多載

Attach(IEnumerable<TEntity>)

在物件內容中,定義物件與相關物件之集合之間的關聯性。Defines relationships between an object and a collection of related objects in an object context.

Attach(TEntity)

在物件內容中,定義兩個所附加物件之間的關聯性。Defines a relationship between two attached objects in an object context.

Attach(IEnumerable<TEntity>)

在物件內容中,定義物件與相關物件之集合之間的關聯性。Defines relationships between an object and a collection of related objects in an object context.

public:
 void Attach(System::Collections::Generic::IEnumerable<TEntity> ^ entities);
public void Attach (System.Collections.Generic.IEnumerable<TEntity> entities);
override this.Attach : seq<'Entity (requires 'Entity : null and 'Entity :> System.Data.Objects.DataClasses.IEntityWithRelationships)> -> unit
override this.Attach : seq<'Entity (requires 'Entity : null)> -> unit
Public Sub Attach (entities As IEnumerable(Of TEntity))

參數

entities
IEnumerable<TEntity>

物件內容中與來源物件相關之物件的集合。Collection of objects in the object context that are related to the source object.

例外狀況

entities 集合為 nullentities collection is null.

來源物件或 entities 集合中的物件是 null 或者不處於 UnchangedModified 狀態中。The source object or an object in the entities collection is null or is not in an Unchanged or Modified state.

-或--or-

關聯性無法根據 [ 艾德作品銷售模型 EDM 中繼資料] 來定義。The relationship cannot be defined based on the Adventure Works Sales Model EDM metadata. 如果概念結構描述中的關聯不支援這兩個型別之間的關聯性,就可能會發生這種情況。This can occur when the association in the conceptual schema does not support a relationship between the two types.

備註

Attach 方法是用來在來源物件與相關物件的集合已經存在物件內容中時,定義物件與相關物件之集合之間的關聯性。The Attach method is used to define relationships between an object and a collection of related objects when both the source object and the collection of related objects already exist in the object context. 若要在已經定義關聯性的位置附加物件或物件圖形,請針對 Attach 呼叫 ObjectContext 方法。To attach an object or an object graph where the relationships are already defined, call the Attach method on the ObjectContext. 若要建立與來源物件相關的新物件,請針對 Add 呼叫 EntityCollection<TEntity> 方法。To create a new object that is related to the source object, call the Add method on the EntityCollection<TEntity>. 如需詳細資訊,請參閱 附加和卸離物件For more information, see Attaching and Detaching Objects.

如果集合已經填滿或部分填滿,Attach 方法就會合併現有的實體與給定的實體。If the collection is already filled or partially filled, the Attach method merges existing entities with the given entities. 給定的實體不會被視為相關實體的完整集合。The given entities are not assumed to be the complete set of related entities.

所有傳入的實體都必須處於 UnchangedModified 狀態中。All entities passed in must be in the Unchanged or Modified state. 只有當狀態管理員已經在追蹤關聯性執行個體時,才允許使用處於 Deleted 狀態中的物件。Objects in the Deleted state are only allowed when the state manager is already tracking the relationship instance.

適用於

Attach(TEntity)

在物件內容中,定義兩個所附加物件之間的關聯性。Defines a relationship between two attached objects in an object context.

public:
 void Attach(TEntity entity);
public void Attach (TEntity entity);
override this.Attach : 'Entity -> unit
Public Sub Attach (entity As TEntity)

參數

entity
TEntity

正在附加的物件。The object being attached.

例外狀況

entitynull 時。When the entity is null.

entity 無法與來源物件相關聯時。When the entity cannot be related to the source object. 如果概念結構描述中的關聯不支援這兩個型別之間的關聯性,就可能會發生這種情況。This can occur when the association in the conceptual schema does not support a relationship between the two types.

-或--or-

當物件是 null 或者不處於 UnchangedModified 狀態中時。When either object is null or is not in an Unchanged or Modified state.

範例

這個範例是以「 艾德作品」銷售模型為基礎。This example is based on the Adventure Works Sales Model. 若要執行此範例中的程式碼,您必須已經將 AdventureWorks Sales Model 加入到專案中,並設定您的專案使用 Entity Framework。To run the code in this example, you must have already added the AdventureWorks Sales Model to your project and configured your project to use the Entity Framework. 若要這樣做,請完成 how to:手動設定 Entity Framework 專案 和 how To:手動定義模型和對應檔中的程式。To do this, complete the procedures in How to: Manually Configure an Entity Framework Project and How to: Manually Define the Model and Mapping Files.

這個範例會將卸離的 SalesOrderDetail 物件集合和卸離的 SalesOrderHeader 物件附加至物件內容,然後定義 SalesOrderHeader 物件和每個物件之間的關聯性 SalesOrderDetailThis example attaches a collection of detached SalesOrderDetail objects and a detached SalesOrderHeader object to an object context, and then defines the relationships between the SalesOrderHeader object and each SalesOrderDetail object.

private static void AttachRelatedObjects(
    ObjectContext currentContext,
    SalesOrderHeader detachedOrder,
    List<SalesOrderDetail> detachedItems)
{
    // 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.SalesOrderDetails.Attach(item);
    }
}
Private Shared Sub AttachRelatedObjects(ByVal currentContext As ObjectContext, ByVal detachedOrder As SalesOrderHeader, ByVal detachedItems As List(Of SalesOrderDetail))
    ' 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.SalesOrderDetails.Attach(item)
    Next
End Sub

備註

Attach 方法是用來在兩個物件已經存在物件內容中時,定義這兩個物件之間的關聯性。The Attach method is used to define relationships between two objects when both objects already exist in the object context. 若要在已經定義關聯性的位置附加物件或物件圖形,請針對 Attach 呼叫 ObjectContext 方法。To attach an object or an object graph where the relationships are already defined, call the Attach method on the ObjectContext. 若要建立與來源物件相關的新物件,請針對 Add 呼叫 EntityCollection<TEntity> 方法。To create a new object that is related to the source object, call the Add method on the EntityCollection<TEntity>. 如需詳細資訊,請參閱 附加和卸離物件For more information, see Attaching and Detaching Objects.

如果 EntityCollection<TEntity> 已經載入物件,Attach 方法就會合併物件與 EntityCollection<TEntity> 中的現有物件。If the EntityCollection<TEntity> already has loaded objects, the Attach method merges the object together with the existing objects in the EntityCollection<TEntity>.

所附加的物件不會被視為相關實體物件的完整集合。The attached object is not assumed to be the complete set of related entity objects.

與這個 EntityCollection<TEntity> 相關聯的物件以及附加至此物件的所有物件都必須處於 UnchangedModified 狀態中。The object associated with this EntityCollection<TEntity> and all objects being attached to it must be in an Unchanged or Modified state.

只有當 Deleted 已經在追蹤關聯性執行個體時,才能附加處於 ObjectStateManager 狀態中的物件。Objects in the Deleted state can only be attached when the ObjectStateManager is already tracking the relationship instance.

適用於