Table of Contents

Class RelationalCommand

Namespace
Microsoft.EntityFrameworkCore.Storage
Assembly
Microsoft.EntityFrameworkCore.Relational.dll

A command to be executed against a relational database.

This type is typically used by database providers (and other extensions). It is generally not used in application code.

public class RelationalCommand : IRelationalCommand, IRelationalCommandTemplate
Inheritance
RelationalCommand
Implements
Inherited Members

Remarks

See Implementation of database providers and extensions for more information and examples.

Constructors

RelationalCommand(RelationalCommandBuilderDependencies, string, IReadOnlyList<IRelationalParameter>)

Constructs a new RelationalCommand.

This type is typically used by database providers (and other extensions). It is generally not used in application code.

public RelationalCommand(RelationalCommandBuilderDependencies dependencies, string commandText, IReadOnlyList<IRelationalParameter> parameters)

Parameters

dependencies RelationalCommandBuilderDependencies

Service dependencies.

commandText string

The text of the command to be executed.

parameters IReadOnlyList<IRelationalParameter>

Parameters for the command.

Properties

CommandText

Gets the command text to be executed.

public virtual string CommandText { get; }

Property Value

string

Dependencies

Relational provider-specific dependencies for this service.

protected virtual RelationalCommandBuilderDependencies Dependencies { get; }

Property Value

RelationalCommandBuilderDependencies

Parameters

Gets the parameters for the command.

public virtual IReadOnlyList<IRelationalParameter> Parameters { get; }

Property Value

IReadOnlyList<IRelationalParameter>

Methods

CreateDbCommand(RelationalCommandParameterObject, Guid, DbCommandMethod)

Called by the execute methods to create a DbCommand for the given DbConnection and configure timeouts and transactions.

This method is typically used by database providers (and other extensions). It is generally not used in application code.

public virtual DbCommand CreateDbCommand(RelationalCommandParameterObject parameterObject, Guid commandId, DbCommandMethod commandMethod)

Parameters

parameterObject RelationalCommandParameterObject

Parameters for this method.

commandId Guid

The command correlation ID.

commandMethod DbCommandMethod

The method that will be called on the created command.

Returns

DbCommand

The created command.

CreateRelationalDataReader()

Creates a new RelationalDataReader to be used by ExecuteReader(RelationalCommandParameterObject) and ExecuteReaderAsync(RelationalCommandParameterObject, CancellationToken). The returned RelationalDataReader may get used more for multiple queries, and will be re-initialized each time via Initialize(IRelationalConnection, DbCommand, DbDataReader, Guid, IRelationalCommandDiagnosticsLogger?).

This method is typically used by database providers (and other extensions). It is generally not used in application code.

protected virtual RelationalDataReader CreateRelationalDataReader()

Returns

RelationalDataReader

The created RelationalDataReader.

ExecuteNonQuery(RelationalCommandParameterObject)

Executes the command with no results.

public virtual int ExecuteNonQuery(RelationalCommandParameterObject parameterObject)

Parameters

parameterObject RelationalCommandParameterObject

Parameters for this method.

Returns

int

The number of rows affected.

ExecuteNonQueryAsync(RelationalCommandParameterObject, CancellationToken)

Asynchronously executes the command with no results.

public virtual Task<int> ExecuteNonQueryAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken = default)

Parameters

parameterObject RelationalCommandParameterObject

Parameters for this method.

cancellationToken CancellationToken

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

Returns

Task<int>

A task that represents the asynchronous operation. The task result contains the number of rows affected.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

ExecuteReader(RelationalCommandParameterObject)

Executes the command with a RelationalDataReader result.

public virtual RelationalDataReader ExecuteReader(RelationalCommandParameterObject parameterObject)

Parameters

parameterObject RelationalCommandParameterObject

Parameters for this method.

Returns

RelationalDataReader

The result of the command.

ExecuteReaderAsync(RelationalCommandParameterObject, CancellationToken)

Asynchronously executes the command with a RelationalDataReader result.

public virtual Task<RelationalDataReader> ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken = default)

Parameters

parameterObject RelationalCommandParameterObject

Parameters for this method.

cancellationToken CancellationToken

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

Returns

Task<RelationalDataReader>

A task that represents the asynchronous operation. The task result contains the result of the command.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

ExecuteScalar(RelationalCommandParameterObject)

Executes the command with a single scalar result.

public virtual object? ExecuteScalar(RelationalCommandParameterObject parameterObject)

Parameters

parameterObject RelationalCommandParameterObject

Parameters for this method.

Returns

object

The result of the command.

ExecuteScalarAsync(RelationalCommandParameterObject, CancellationToken)

Asynchronously executes the command with a single scalar result.

public virtual Task<object?> ExecuteScalarAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken = default)

Parameters

parameterObject RelationalCommandParameterObject

Parameters for this method.

cancellationToken CancellationToken

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

Returns

Task<object>

A task that represents the asynchronous operation. The task result contains the result of the command.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

PopulateFrom(IRelationalCommandTemplate)

Populates this command from the provided commandTemplate.

public virtual void PopulateFrom(IRelationalCommandTemplate commandTemplate)

Parameters

commandTemplate IRelationalCommandTemplate

A template command from which the command text and parameters will be copied.