Table of Contents

Interface IRelationalConnection

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

Represents a connection with a relational database.

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

public interface IRelationalConnection : IRelationalTransactionManager, IDbContextTransactionManager, IResettableService, IDisposable, IAsyncDisposable
Inherited Members

Remarks

The service lifetime is Scoped. This means that each Microsoft.EntityFrameworkCore.DbContext instance will use its own instance of this service. The implementation may depend on other services registered with any lifetime. The implementation does not need to be thread-safe.

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

Properties

CommandTimeout

Gets the timeout for executing a command against the database.

int? CommandTimeout { get; set; }

Property Value

int?

ConnectionId

Gets the connection identifier.

Guid ConnectionId { get; }

Property Value

Guid

ConnectionString

Gets or sets the connection string for the database.

string? ConnectionString { get; set; }

Property Value

string

Context

The Microsoft.EntityFrameworkCore.DbContext currently in use, or null if not known.

DbContext Context { get; }

Property Value

DbContext

CurrentTransaction

Gets the current transaction.

IDbContextTransaction? CurrentTransaction { get; }

Property Value

IDbContextTransaction

DbConnection

Gets or sets the underlying DbConnection used to connect to the database.

DbConnection DbConnection { get; set; }

Property Value

DbConnection

Remarks

The connection can only be changed when the existing connection, if any, is not open.

Note that the connection must be disposed by application code since it was not created by Entity Framework.

Methods

Close()

Closes the connection to the database.

bool Close()

Returns

bool

true if the underlying connection was actually closed; false otherwise.

CloseAsync()

Closes the connection to the database.

Task<bool> CloseAsync()

Returns

Task<bool>

A task that represents the asynchronous operation, with a value of true if the connection was actually closed.

Open(bool)

Opens the connection to the database.

bool Open(bool errorsExpected = false)

Parameters

errorsExpected bool

Indicate if the connection errors are expected and should be logged as debug message.

Returns

bool

true if the underlying connection was actually opened; false otherwise.

OpenAsync(CancellationToken, bool)

Asynchronously opens the connection to the database.

Task<bool> OpenAsync(CancellationToken cancellationToken, bool errorsExpected = false)

Parameters

cancellationToken CancellationToken

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

errorsExpected bool

Indicate if the connection errors are expected and should be logged as debug message.

Returns

Task<bool>

A task that represents the asynchronous operation, with a value of true if the connection was actually opened.

Exceptions

OperationCanceledException

If the CancellationToken is canceled.

RentCommand()

Rents a relational command that can be executed with this connection.

IRelationalCommand RentCommand()

Returns

IRelationalCommand

A relational command that can be executed with this connection.

ReturnCommand(IRelationalCommand)

Returns a relational command to this connection, so that it can be reused in the future.

void ReturnCommand(IRelationalCommand command)

Parameters

command IRelationalCommand