ObjectStateManager.GetObjectStateEntry Método

Definição

Retorna um ObjectStateEntry para uma entrada de objeto ou relação específica.Returns an ObjectStateEntry for a specific object or relationship entry.

Sobrecargas

GetObjectStateEntry(EntityKey)

Retorna um ObjectStateEntry para o objeto ou para a entrada de relação com a chave especificada.Returns an ObjectStateEntry for the object or relationship entry with the specified key.

GetObjectStateEntry(Object)

Retorna um ObjectStateEntry para o objeto especificado.Returns an ObjectStateEntry for the specified object.

GetObjectStateEntry(EntityKey)

Retorna um ObjectStateEntry para o objeto ou para a entrada de relação com a chave especificada.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

Parâmetros

Retornos

ObjectStateEntry

O ObjectStateEntry correspondente para o EntityKey fornecido.The corresponding ObjectStateEntry for the given EntityKey.

Exceções

Quando key é null.When key is null.

Quando o key especificado não pode ser encontrado no gerenciador de estado.When the specified key cannot be found in the state manager.

Nenhuma entidade com o EntityKey especificado existe no ObjectStateManager.No entity with the specified EntityKey exists in the ObjectStateManager.

Exemplos

O exemplo neste tópico é baseado no Microsoft SQL Server exemplos de produto: banco de dados.The example in this topic is based on the Microsoft SQL Server Product Samples: Database. O exemplo obtém o ObjectStateEntry para o fornecido EntityKey do ObjectStateManager .The example gets the ObjectStateEntry for the given EntityKey from the ObjectStateManager. Em seguida, ele obtém o valor atual da SalesOrderHeader.PurchaseOrderNumber propriedade, altera o valor da propriedade e enumera por meio da coleção de propriedades modificadas.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

Comentários

Use o TryGetObjectStateEntry(EntityKey, ObjectStateEntry) método para retornar um ObjectStateEntry objeto sem precisar lidar com o InvalidOperationException gerado pelo GetObjectStateEntry(EntityKey) método.Use the TryGetObjectStateEntry(EntityKey, ObjectStateEntry) method to return an ObjectStateEntry object without having to handle the InvalidOperationException raised by the GetObjectStateEntry(EntityKey) method.

Aplica-se a

GetObjectStateEntry(Object)

Retorna um ObjectStateEntry para o objeto especificado.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

Parâmetros

entity
Object

O Object ao qual o ObjectStateEntry recuperado pertence.The Object to which the retrieved ObjectStateEntry belongs.

Retornos

ObjectStateEntry

O ObjectStateEntry correspondente para o Object fornecido.The corresponding ObjectStateEntry for the given Object.

Exceções

Não existe nenhuma entidade com o Object especificado no ObjectStateManager.No entity for the specified Object exists in the ObjectStateManager.

Comentários

Use o TryGetObjectStateEntry(Object, ObjectStateEntry) método para retornar um ObjectStateEntry objeto sem precisar lidar com o InvalidOperationException gerado pelo GetObjectStateEntry(Object) método.Use the TryGetObjectStateEntry(Object, ObjectStateEntry) method to return an ObjectStateEntry object without having to handle the InvalidOperationException raised by the GetObjectStateEntry(Object) method.

Aplica-se a