DbSet<TEntity>.Find Method

[This page is specific to the Entity Framework version 6. The latest version is available as the 'Entity Framework' NuGet package. For more information about Entity Framework, see msdn.com/data/ef.]

Finds an entity with the given primary key values. If an entity with the given primary key values exists in the context, then it is returned immediately without making a request to the store. Otherwise, a request is made to the store for an entity with the given primary key values and this entity, if found, is attached to the context and returned. If no entity is found in the context or the store, then null is returned.

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

Syntax

'Declaration
Public Overridable Function Find ( _
    ParamArray keyValues As Object() _
) As TEntity
'Usage
Dim instance As DbSet 
Dim keyValues As Object()
Dim returnValue As TEntity

returnValue = instance.Find(keyValues)
public virtual TEntity Find(
    params Object[] keyValues
)
public:
virtual TEntity Find(
    ... array<Object^>^ keyValues
)
abstract Find : 
        keyValues:Object[] -> 'TEntity 
override Find : 
        keyValues:Object[] -> 'TEntity 
public function Find(
    ... keyValues : Object[]
) : TEntity

Parameters

  • keyValues
    Type: System.Object[]
    The values of the primary key for the entity to be found.

Return Value

Type: TEntity
The entity found, or null.

Implements

IDbSet<TEntity>.Find(Object[])

Exceptions

Exception Condition
InvalidOperationException

Thrown if multiple entities exist in the context with the primary key values given.

InvalidOperationException

Thrown if the type of entity is not part of the data model for this context.

InvalidOperationException

Thrown if the types of the key values do not match the types of the key values for the entity type to be found.

InvalidOperationException

Thrown if the context has been disposed.

Remarks

The ordering of composite key values is as defined in the EDM, which is in turn as defined in the designer, by the Code First fluent API, or by the DataMember attribute.

See Also

Reference

DbSet<TEntity> Class

System.Data.Entity Namespace