This topic has not yet been rated - Rate this topic

ObjectContext.CreateEntityKey Method

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Creates the entity key for a specific object, or returns the entity key if it already exists.

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

public EntityKey CreateEntityKey(
	string entitySetName,
	Object entity
)

Parameters

entitySetName
Type: System.String
The fully qualified name of the entity set to which the entity object belongs.
entity
Type: System.Object
The object for which the entity key is being retrieved.

Return Value

Type: System.Data.EntityKey
The EntityKey of the object.
Exception Condition
ArgumentNullException

When either parameter is null.

ArgumentException

When entitySetName is empty.

-or-

When the type of the entity object does not exist in the entity set.

-or-

When the entitySetName is not fully qualified.

InvalidOperationException

When the entity key cannot be constructed successfully based on the supplied parameters.

If an EntityKey does not exist for the entity, the CreateEntityKey method creates a new key for it.

This method is used to determine whether an object that has the same EntityKey is already attached to the ObjectContext. If an object that has the same EntityKey is already attached, an exception is raised. Use the CreateEntityKey method to try to retrieve the EntityKey of the detached object before calling the Attach method.

The example in this topic is based on the Adventure Works Sales Model. In this example, CreateEntityKey is used to retrieve the entity key of an existing object.


private static void ApplyItemUpdates(SalesOrderDetail updatedItem)
{
    // Define an ObjectStateEntry and EntityKey for the current object. 
    EntityKey key = default(EntityKey);
    object originalItem = null;

    using (AdventureWorksEntities context = new AdventureWorksEntities())
    {
        // Create the detached object's entity key. 
        key = context.CreateEntityKey("SalesOrderDetails", updatedItem);

        // Get the original item based on the entity key from the context 
        // or from the database. 
        if (context.TryGetObjectByKey(key, out originalItem))
        {
            // Call the ApplyCurrentValues method to apply changes 
            // from the updated item to the original version. 
            context.ApplyCurrentValues(key.EntitySetName, updatedItem);
        }

        context.SaveChanges();
    }
}


.NET Framework

Supported in: 4.5, 4, 3.5 SP1

.NET Framework Client Profile

Supported in: 4

Windows 8 Release Preview, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 SP2, Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Did you find this helpful?
(1500 characters remaining)