Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
TryGetObjectStateEntry Method (EntityKey, ObjectStateEntry)

ObjectStateManager.TryGetObjectStateEntry Method (EntityKey, ObjectStateEntry%)

Tries to retrieve the corresponding ObjectStateEntry for the object or relationship with the specified EntityKey.

Namespace:  System.Data.Objects
Assembly:  System.Data.Entity (in System.Data.Entity.dll)

public bool TryGetObjectStateEntry(
	EntityKey key,
	out ObjectStateEntry entry
)

Parameters

key
Type: System.Data.EntityKey

The given EntityKey.

entry
Type: System.Data.Objects.ObjectStateEntry%

When this method returns, contains an ObjectStateEntry for the given EntityKey This parameter is passed uninitialized.

Return Value

Type: System.Boolean
A Boolean value that is true if there is a corresponding ObjectStateEntry for the given EntityKey; otherwise, false.

ExceptionCondition
ArgumentNullException

A null (Nothing in Visual Basic) value is provided for key.

Use TryGetObjectStateEntry(EntityKey, ObjectStateEntry) to return an ObjectStateEntry without having to handle the InvalidOperationException raised by the GetObjectStateEntry(EntityKey) method.

The examples in this topic are based on the Adventure Works Sales Model.

The example attempts to retrieve the corresponding ObjectStateEntry for the given EntityKey.

int orderId = 43680;

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

        SalesOrderHeader order =
            advWorksContext.SalesOrderHeader.Where
            ("it.SalesOrderID = @id", new ObjectParameter(
             "id", orderId)).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");
        }
    }
    catch (EntitySqlException ex)
    {
        Console.WriteLine(ex.ToString());
    }
    catch (EntityCommandExecutionException ex)
    {
        Console.WriteLine(ex.ToString());
    }
}

The following example uses the TryGetObjectStateEntry(EntityKey, ObjectStateEntry) method on the returned ObjectStateManager to get an object based on its entity key.

private static void ApplyItemUpdates(SalesOrderDetail originalItem,
    SalesOrderDetail updatedItem)
{
    using (AdventureWorksEntities advWorksContext = 
        new AdventureWorksEntities())
    {
        try
        {
            // Attach the original item to the object context.
            advWorksContext.Attach(originalItem);

            // Call the ApplyPropertyChanges method to apply changes 
            // from the updated item to the original version.
            advWorksContext.ApplyPropertyChanges("SalesOrderDetail",
                updatedItem);

            advWorksContext.SaveChanges();
        }
        catch (InvalidOperationException ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1

Community Additions

ADD
Show:
© 2015 Microsoft