ObjectStateManager.GetObjectStateEntry メソッド

定義

特定のオブジェクトまたはリレーションシップ エントリの ObjectStateEntry を返します。Returns an ObjectStateEntry for a specific object or relationship entry.

オーバーロード

GetObjectStateEntry(EntityKey)

指定されたキーのオブジェクトまたはリレーションシップ エントリの ObjectStateEntry を返します。Returns an ObjectStateEntry for the object or relationship entry with the specified key.

GetObjectStateEntry(Object)

指定されたオブジェクトの ObjectStateEntry を返します。Returns an ObjectStateEntry for the specified object.

GetObjectStateEntry(EntityKey)

指定されたキーのオブジェクトまたはリレーションシップ エントリの ObjectStateEntry を返します。Returns an ObjectStateEntry for the object or relationship entry with the specified key.

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 に対応する EntityKeyThe corresponding ObjectStateEntry for the given EntityKey.

例外

keynull の場合。When key is null.

指定された key が状態マネージャーに見つからない場合。When the specified key cannot be found in the state manager.

EntityKey には、指定した ObjectStateManager を持つエンティティが存在しません。No entity with the specified EntityKey exists in the ObjectStateManager.

このトピックの例は、 AdventureWorks Sales Modelに基づいています。The example in this topic is based on the AdventureWorks Sales Model. この例では、指定された ObjectStateEntryEntityKeyObjectStateManager から取得します。The example gets the ObjectStateEntry for the given EntityKey from the ObjectStateManager. 次に、 SalesOrderHeader.PurchaseOrderNumberプロパティの現在の値を取得し、プロパティの値を変更し、変更されたプロパティのコレクションを通じて列挙します。Then it gets the current value of the SalesOrderHeader.PurchaseOrderNumber property, changes the property's value, and enumerates through the collection of modified properties.

// 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());
    }
}
' Specify the order to update. 
Dim orderId As Integer = 43680

Using context As New AdventureWorksEntities()
    Try
        Dim 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)); 
        Dim detail = New SalesOrderDetail With
        {
            .SalesOrderID = 0,
            .SalesOrderDetailID = 0,
            .OrderQty = 2,
            .ProductID = 750,
            .SpecialOfferID = 1,
            .UnitPrice = CDec(2171.2942),
            .UnitPriceDiscount = 0,
            .LineTotal = 0,
            .rowguid = Guid.NewGuid(),
            .ModifiedDate = DateTime.Now
        }

        order.SalesOrderDetails.Add(detail)

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

        ' Save changes in the object context to the database. 
        Dim changes As Integer = 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. 
        For Each item As SalesOrderDetail In order.SalesOrderDetails
            Console.WriteLine("Item ID: {0}", item.SalesOrderDetailID.ToString())
            Console.WriteLine("Product: {0}", item.ProductID.ToString())
            Console.WriteLine("Quantity: {0}", item.OrderQty.ToString())
        Next
    Catch ex As UpdateException
        Console.WriteLine(ex.ToString())
    End Try
End Using

注釈

メソッドを使用して、 ObjectStateEntry GetObjectStateEntry(EntityKey)メソッドによって発生しInvalidOperationExceptionたを処理せずにオブジェクトを返します。 TryGetObjectStateEntry(EntityKey, ObjectStateEntry)Use the TryGetObjectStateEntry(EntityKey, ObjectStateEntry) method to return an ObjectStateEntry object without having to handle the InvalidOperationException raised by the GetObjectStateEntry(EntityKey) method.

GetObjectStateEntry(Object)

指定されたオブジェクトの ObjectStateEntry を返します。Returns an ObjectStateEntry for the specified object.

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 が属する ObjectStateEntryThe Object to which the retrieved ObjectStateEntry belongs.

戻り値

指定した ObjectStateEntry に対応する ObjectThe corresponding ObjectStateEntry for the given Object.

例外

Object には、指定された ObjectStateManager のエンティティがありません。No entity for the specified Object exists in the ObjectStateManager.

注釈

メソッドを使用して、 ObjectStateEntry GetObjectStateEntry(Object)メソッドによって発生しInvalidOperationExceptionたを処理せずにオブジェクトを返します。 TryGetObjectStateEntry(Object, ObjectStateEntry)Use the TryGetObjectStateEntry(Object, ObjectStateEntry) method to return an ObjectStateEntry object without having to handle the InvalidOperationException raised by the GetObjectStateEntry(Object) method.

適用対象