Share via


DbContext.FindAsync Method

Definition

Overloads

FindAsync(Type, Object[], CancellationToken)

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database. Otherwise, a query is made to the database 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, then null is returned.

FindAsync(Type, Object[])

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database. Otherwise, a query is made to the database 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, then null is returned.

FindAsync<TEntity>(Object[])

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database. Otherwise, a query is made to the database 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, then null is returned.

FindAsync<TEntity>(Object[], CancellationToken)

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database. Otherwise, a query is made to the database 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, then null is returned.

FindAsync(Type, Object[], CancellationToken)

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database. Otherwise, a query is made to the database 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, then null is returned.

public virtual System.Threading.Tasks.Task<object> FindAsync (Type entityType, object[] keyValues, System.Threading.CancellationToken cancellationToken);
public virtual System.Threading.Tasks.ValueTask<object> FindAsync (Type entityType, object[] keyValues, System.Threading.CancellationToken cancellationToken);
public virtual System.Threading.Tasks.ValueTask<object?> FindAsync (Type entityType, object?[]? keyValues, System.Threading.CancellationToken cancellationToken);
abstract member FindAsync : Type * obj[] * System.Threading.CancellationToken -> System.Threading.Tasks.Task<obj>
override this.FindAsync : Type * obj[] * System.Threading.CancellationToken -> System.Threading.Tasks.Task<obj>
abstract member FindAsync : Type * obj[] * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<obj>
override this.FindAsync : Type * obj[] * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<obj>
Public Overridable Function FindAsync (entityType As Type, keyValues As Object(), cancellationToken As CancellationToken) As Task(Of Object)
Public Overridable Function FindAsync (entityType As Type, keyValues As Object(), cancellationToken As CancellationToken) As ValueTask(Of Object)

Parameters

entityType
Type

The type of entity to find.

keyValues
Object[]

The values of the primary key for the entity to be found.

cancellationToken
CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

The entity found, or null.

Exceptions

Remarks

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See Using Find and FindAsync for more information and examples.

Applies to

FindAsync(Type, Object[])

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database. Otherwise, a query is made to the database 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, then null is returned.

public virtual System.Threading.Tasks.Task<object> FindAsync (Type entityType, params object[] keyValues);
public virtual System.Threading.Tasks.ValueTask<object> FindAsync (Type entityType, params object[] keyValues);
public virtual System.Threading.Tasks.ValueTask<object?> FindAsync (Type entityType, params object?[]? keyValues);
abstract member FindAsync : Type * obj[] -> System.Threading.Tasks.Task<obj>
override this.FindAsync : Type * obj[] -> System.Threading.Tasks.Task<obj>
abstract member FindAsync : Type * obj[] -> System.Threading.Tasks.ValueTask<obj>
override this.FindAsync : Type * obj[] -> System.Threading.Tasks.ValueTask<obj>
Public Overridable Function FindAsync (entityType As Type, ParamArray keyValues As Object()) As Task(Of Object)
Public Overridable Function FindAsync (entityType As Type, ParamArray keyValues As Object()) As ValueTask(Of Object)

Parameters

entityType
Type

The type of entity to find.

keyValues
Object[]

The values of the primary key for the entity to be found.

Returns

The entity found, or null.

Remarks

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See Using Find and FindAsync for more information and examples.

Applies to

FindAsync<TEntity>(Object[])

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database. Otherwise, a query is made to the database 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, then null is returned.

public virtual System.Threading.Tasks.Task<TEntity> FindAsync<TEntity> (params object[] keyValues) where TEntity : class;
public virtual System.Threading.Tasks.ValueTask<TEntity> FindAsync<TEntity> (params object[] keyValues) where TEntity : class;
public virtual System.Threading.Tasks.ValueTask<TEntity?> FindAsync<TEntity> (params object?[]? keyValues) where TEntity : class;
abstract member FindAsync : obj[] -> System.Threading.Tasks.Task<'Entity (requires 'Entity : null)> (requires 'Entity : null)
override this.FindAsync : obj[] -> System.Threading.Tasks.Task<'Entity (requires 'Entity : null)> (requires 'Entity : null)
abstract member FindAsync : obj[] -> System.Threading.Tasks.ValueTask<'Entity (requires 'Entity : null)> (requires 'Entity : null)
override this.FindAsync : obj[] -> System.Threading.Tasks.ValueTask<'Entity (requires 'Entity : null)> (requires 'Entity : null)
Public Overridable Function FindAsync(Of TEntity As Class) (ParamArray keyValues As Object()) As Task(Of TEntity)
Public Overridable Function FindAsync(Of TEntity As Class) (ParamArray keyValues As Object()) As ValueTask(Of TEntity)

Type Parameters

TEntity

The type of entity to find.

Parameters

keyValues
Object[]

The values of the primary key for the entity to be found.

Returns

Task<TEntity>
ValueTask<TEntity>

The entity found, or null.

Remarks

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See Using Find and FindAsync for more information and examples.

Applies to

FindAsync<TEntity>(Object[], CancellationToken)

Finds an entity with the given primary key values. If an entity with the given primary key values is being tracked by the context, then it is returned immediately without making a request to the database. Otherwise, a query is made to the database 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, then null is returned.

public virtual System.Threading.Tasks.Task<TEntity> FindAsync<TEntity> (object[] keyValues, System.Threading.CancellationToken cancellationToken) where TEntity : class;
public virtual System.Threading.Tasks.ValueTask<TEntity> FindAsync<TEntity> (object[] keyValues, System.Threading.CancellationToken cancellationToken) where TEntity : class;
public virtual System.Threading.Tasks.ValueTask<TEntity?> FindAsync<TEntity> (object?[]? keyValues, System.Threading.CancellationToken cancellationToken) where TEntity : class;
abstract member FindAsync : obj[] * System.Threading.CancellationToken -> System.Threading.Tasks.Task<'Entity (requires 'Entity : null)> (requires 'Entity : null)
override this.FindAsync : obj[] * System.Threading.CancellationToken -> System.Threading.Tasks.Task<'Entity (requires 'Entity : null)> (requires 'Entity : null)
abstract member FindAsync : obj[] * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<'Entity (requires 'Entity : null)> (requires 'Entity : null)
override this.FindAsync : obj[] * System.Threading.CancellationToken -> System.Threading.Tasks.ValueTask<'Entity (requires 'Entity : null)> (requires 'Entity : null)
Public Overridable Function FindAsync(Of TEntity As Class) (keyValues As Object(), cancellationToken As CancellationToken) As Task(Of TEntity)
Public Overridable Function FindAsync(Of TEntity As Class) (keyValues As Object(), cancellationToken As CancellationToken) As ValueTask(Of TEntity)

Type Parameters

TEntity

The type of entity to find.

Parameters

keyValues
Object[]

The values of the primary key for the entity to be found.

cancellationToken
CancellationToken

A CancellationToken to observe while waiting for the task to complete.

Returns

Task<TEntity>
ValueTask<TEntity>

The entity found, or null.

Exceptions

Remarks

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See Using Find and FindAsync for more information and examples.

Applies to