共用方式為


ObjectStateManager.GetObjectStateEntry 方法

定義

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

多載

GetObjectStateEntry(EntityKey)

傳回具有指定之索引鍵的物件或關聯性項目的 ObjectStateEntry

GetObjectStateEntry(Object)

傳回指定之物件的 ObjectStateEntry

GetObjectStateEntry(EntityKey)

傳回具有指定之索引鍵的物件或關聯性項目的 ObjectStateEntry

public:
 System::Data::Objects::ObjectStateEntry ^ GetObjectStateEntry(System::Data::EntityKey ^ key);
public System.Data.Objects.ObjectStateEntry GetObjectStateEntry (System.Data.EntityKey key);
member this.GetObjectStateEntry : System.Data.EntityKey -> System.Data.Objects.ObjectStateEntry
Public Function GetObjectStateEntry (key As EntityKey) As ObjectStateEntry

參數

傳回

給定 ObjectStateEntry 的對應 EntityKey

例外狀況

keynull 時。

當狀態管理員中找不到指定的 key 時。

具有指定之 EntityKey 的實體不存在 ObjectStateManager 中。

範例

這個範例會從 取得ObjectStateEntry所指定 EntityKeyObjectStateManager 然後,它會取得屬性的 SalesOrderHeader.PurchaseOrderNumber 目前值、變更屬性值,並透過修改屬性的集合列舉。

// Specify the order to update.
int orderId = 43680;

using (AdventureWorksEntities context =
    new AdventureWorksEntities())
{
    try
    {
        var order = (from o in context.SalesOrderHeaders
                     where o.SalesOrderID == orderId
                     select o).First();

        // Change the status of an existing order.
        order.Status = 1;

        // Delete the first item in the order.
        context.DeleteObject(order.SalesOrderDetails.First());

        // Create a new SalesOrderDetail object.
        // You can use the static CreateObjectName method (the Entity Framework
        // adds this method to the generated entity types) instead of the new operator:
        // SalesOrderDetail.CreateSalesOrderDetail(1, 0, 2, 750, 1, (decimal)2171.2942, 0, 0,
        //                                         Guid.NewGuid(), DateTime.Today));
        SalesOrderDetail detail = new SalesOrderDetail
        {
            SalesOrderID = 0,
            SalesOrderDetailID = 0,
            OrderQty = 2,
            ProductID = 750,
            SpecialOfferID = 1,
            UnitPrice = (decimal)2171.2942,
            UnitPriceDiscount = 0,
            LineTotal = 0,
            rowguid = Guid.NewGuid(),
            ModifiedDate = DateTime.Now
        };
        order.SalesOrderDetails.Add(detail);

        // Get the ObjectStateEntry for the order.
        ObjectStateEntry stateEntry =
            context.ObjectStateManager
            .GetObjectStateEntry(order);
        Console.WriteLine("State before SaveChanges() is called: {0}",
            stateEntry.State.ToString());

        // Save changes in the object context to the database.
        int changes = context.SaveChanges();

        Console.WriteLine("State after SaveChanges() is called: {0}",
            stateEntry.State.ToString());

        Console.WriteLine(changes.ToString() + " changes saved!");
        Console.WriteLine("Updated item for order ID: "
            + order.SalesOrderID.ToString());

        // Iterate through the collection of SalesOrderDetail items.
        foreach (SalesOrderDetail item in order.SalesOrderDetails)
        {
            Console.WriteLine("Item ID: "
                + item.SalesOrderDetailID.ToString() + "  Product: "
                + item.ProductID.ToString() + "  Quantity: "
                + item.OrderQty.ToString());
        }
    }
    catch (UpdateException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

備註

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

適用於

GetObjectStateEntry(Object)

傳回指定之物件的 ObjectStateEntry

public:
 System::Data::Objects::ObjectStateEntry ^ GetObjectStateEntry(System::Object ^ entity);
public System.Data.Objects.ObjectStateEntry GetObjectStateEntry (object entity);
member this.GetObjectStateEntry : obj -> System.Data.Objects.ObjectStateEntry
Public Function GetObjectStateEntry (entity As Object) As ObjectStateEntry

參數

entity
Object

已擷取之 Object 所屬的 ObjectStateEntry

傳回

給定 ObjectStateEntry 的對應 Object

例外狀況

指定之 Object 的實體不存在 ObjectStateManager 中。

備註

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

適用於