ObjectStateManager.TryGetObjectStateEntry 方法

定義

嘗試傳回特定物件或關聯性項目的 ObjectStateEntry 物件。

多載

TryGetObjectStateEntry(EntityKey, ObjectStateEntry)

嘗試擷取具有指定之 ObjectStateEntry 的物件或關聯性的對應 EntityKey

TryGetObjectStateEntry(Object, ObjectStateEntry)

嘗試擷取指定之 ObjectStateEntry 的對應 Object

TryGetObjectStateEntry(EntityKey, ObjectStateEntry)

嘗試擷取具有指定之 ObjectStateEntry 的物件或關聯性的對應 EntityKey

public:
 bool TryGetObjectStateEntry(System::Data::EntityKey ^ key, [Runtime::InteropServices::Out] System::Data::Objects::ObjectStateEntry ^ % entry);
public bool TryGetObjectStateEntry (System.Data.EntityKey key, out System.Data.Objects.ObjectStateEntry entry);
member this.TryGetObjectStateEntry : System.Data.EntityKey * ObjectStateEntry -> bool
Public Function TryGetObjectStateEntry (key As EntityKey, ByRef entry As ObjectStateEntry) As Boolean

參數

key
EntityKey

給定的 EntityKey

entry
ObjectStateEntry

當這個方法傳回時,就會包含給定 ObjectStateEntryEntityKey。這個參數會在未初始化的狀態下傳遞。

傳回

布林值,如果指定的 true 具有對應的 ObjectStateEntry,則為 EntityKey,否則為 false

例外狀況

null (在 Visual Basic 中為 Nothing) 值是 key 所提供的。

範例

下列範例會嘗試擷取指定 EntityKey之的對應 ObjectStateEntry

int orderId = 43680;

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    ObjectStateManager objectStateManager = context.ObjectStateManager;
    ObjectStateEntry stateEntry = null;

    var order = (from o in context.SalesOrderHeaders
                 where o.SalesOrderID == orderId
                 select o).First();

    // Attempts to retrieve ObjectStateEntry for the given EntityKey.
    bool isPresent = objectStateManager.TryGetObjectStateEntry(((IEntityWithKey)order).EntityKey, out stateEntry);
    if (isPresent)
    {
        Console.WriteLine("The entity was found");
    }
}

下列範例會針對傳回的 TryGetObjectStateEntry(EntityKey, ObjectStateEntry) 使用 ObjectStateManager 方法,以便根據實體索引鍵取得物件。

private static void ApplyItemUpdates(SalesOrderDetail originalItem,
    SalesOrderDetail updatedItem)
{
    using (AdventureWorksEntities context =
        new AdventureWorksEntities())
    {
        context.SalesOrderDetails.Attach(updatedItem);
        // Check if the ID is 0, if it is the item is new.
        // In this case we need to chage the state to Added.
        if (updatedItem.SalesOrderDetailID == 0)
        {
            // Because the ID is generated by the database we do not need to
            // set updatedItem.SalesOrderDetailID.
            context.ObjectStateManager.ChangeObjectState(updatedItem, System.Data.EntityState.Added);
        }
        else
        {
            // If the SalesOrderDetailID is not 0, then the item is not new
            // and needs to be updated. Because we already added the
            // updated object to the context we need to apply the original values.
            // If we attached originalItem to the context
            // we would need to apply the current values:
            // context.ApplyCurrentValues("SalesOrderDetails", updatedItem);
            // Applying current or original values, changes the state
            // of the attached object to Modified.
            context.ApplyOriginalValues("SalesOrderDetails", originalItem);
        }
        context.SaveChanges();
    }
}

備註

您可以使用 TryGetObjectStateEntry(EntityKey, ObjectStateEntry) 來傳回 ObjectStateEntry,而不需要處理 InvalidOperationException 方法所引發的 GetObjectStateEntry(EntityKey)

適用於

TryGetObjectStateEntry(Object, ObjectStateEntry)

嘗試擷取指定之 ObjectStateEntry 的對應 Object

public:
 bool TryGetObjectStateEntry(System::Object ^ entity, [Runtime::InteropServices::Out] System::Data::Objects::ObjectStateEntry ^ % entry);
public bool TryGetObjectStateEntry (object entity, out System.Data.Objects.ObjectStateEntry entry);
member this.TryGetObjectStateEntry : obj * ObjectStateEntry -> bool
Public Function TryGetObjectStateEntry (entity As Object, ByRef entry As ObjectStateEntry) As Boolean

參數

entity
Object

已擷取之 Object 所屬的 ObjectStateEntry

entry
ObjectStateEntry

當這個方法傳回時,就會包含給定 ObjectStateEntryEntityKey。這個參數會在未初始化的狀態下傳遞。

傳回

布林值,如果指定的物件具有對應的 true則為 ObjectStateEntry,否則為 false

備註

您可以使用 TryGetObjectStateEntry(Object, ObjectStateEntry) 方法來傳回 ObjectStateEntry,而不需要處理 InvalidOperationException 方法所引發的 GetObjectStateEntry(Object)

適用於