Share via


DatabaseFacade Class

Definition

Provides access to database related information and operations for a context. Instances of this class are typically obtained from Database and it is not designed to be directly constructed in your application code.

public class DatabaseFacade : Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<IServiceProvider>
public class DatabaseFacade : Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<IServiceProvider>, Microsoft.EntityFrameworkCore.Internal.IDatabaseFacadeDependenciesAccessor
public class DatabaseFacade : Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<IServiceProvider>, Microsoft.EntityFrameworkCore.Storage.IDatabaseFacadeDependenciesAccessor
public class DatabaseFacade : Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<IServiceProvider>, Microsoft.EntityFrameworkCore.Infrastructure.IResettableService, Microsoft.EntityFrameworkCore.Storage.IDatabaseFacadeDependenciesAccessor
type DatabaseFacade = class
    interface IInfrastructure<IServiceProvider>
type DatabaseFacade = class
    interface IInfrastructure<IServiceProvider>
    interface IDatabaseFacadeDependenciesAccessor
type DatabaseFacade = class
    interface IInfrastructure<IServiceProvider>
    interface IDatabaseFacadeDependenciesAccessor
    interface IResettableService
Public Class DatabaseFacade
Implements IInfrastructure(Of IServiceProvider)
Public Class DatabaseFacade
Implements IDatabaseFacadeDependenciesAccessor, IInfrastructure(Of IServiceProvider)
Public Class DatabaseFacade
Implements IDatabaseFacadeDependenciesAccessor, IInfrastructure(Of IServiceProvider), IResettableService
Inheritance
DatabaseFacade
Implements
IInfrastructure<IServiceProvider> Microsoft.EntityFrameworkCore.Internal.IDatabaseFacadeDependenciesAccessor IDatabaseFacadeDependenciesAccessor IResettableService

Constructors

DatabaseFacade(DbContext)

Initializes a new instance of the DatabaseFacade class. Instances of this class are typically obtained from Database and it is not designed to be directly constructed in your application code.

Properties

AutoSavepointsEnabled

Whether a transaction savepoint will be created automatically by SaveChanges() if it is called after a transaction has been manually started with BeginTransaction().

AutoTransactionBehavior

Gets or sets a value indicating whether or not a transaction will be created automatically by SaveChanges() if neither 'BeginTransaction' nor 'UseTransaction' has been called.

AutoTransactionsEnabled
Obsolete.
Obsolete.

Gets or sets a value indicating whether or not a transaction will be created automatically by SaveChanges() if none of the 'BeginTransaction' or 'UseTransaction' methods have been called.

CurrentTransaction

Gets the current IDbContextTransaction being used by the context, or null if no transaction is in use.

ProviderName

Returns the name of the database provider currently in use. The name is typically the name of the provider assembly. It is usually easier to use a sugar method such as Microsoft.EntityFrameworkCore.SqlServerDatabaseFacadeExtensions.IsSqlServer instead of calling this method directly.

Methods

BeginTransaction()

Starts a new transaction.

BeginTransactionAsync(CancellationToken)

Asynchronously starts a new transaction.

CanConnect()

Determines whether or not the database is available and can be connected to.

CanConnectAsync(CancellationToken)

Determines whether or not the database is available and can be connected to.

CommitTransaction()

Applies the outstanding operations in the current transaction to the database.

CommitTransactionAsync(CancellationToken)

Applies the outstanding operations in the current transaction to the database.

CreateExecutionStrategy()

Creates an instance of the configured IExecutionStrategy.

EnsureCreated()

Ensures that the database for the context exists.

EnsureCreatedAsync(CancellationToken)

Ensures that the database for the context exists.

EnsureDeleted()

Ensures that the database for the context does not exist. If it does not exist, no action is taken. If it does exist then the database is deleted.

Warning: The entire database is deleted, and no effort is made to remove just the database objects that are used by the model for this context.

EnsureDeletedAsync(CancellationToken)

Asynchronously ensures that the database for the context does not exist. If it does not exist, no action is taken. If it does exist then the database is deleted.

Warning: The entire database is deleted, and no effort is made to remove just the database objects that are used by the model for this context.

RollbackTransaction()

Discards the outstanding operations in the current transaction.

RollbackTransactionAsync(CancellationToken)

Discards the outstanding operations in the current transaction.

Explicit Interface Implementations

IDatabaseFacadeDependenciesAccessor.Context

This is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release. You should only use it directly in your code with extreme caution and knowing that doing so can result in application failures when updating to a new Entity Framework Core release.

IDatabaseFacadeDependenciesAccessor.Dependencies

This is an internal API that supports the Entity Framework Core infrastructure and not subject to the same compatibility standards as public APIs. It may be changed or removed without notice in any release. You should only use it directly in your code with extreme caution and knowing that doing so can result in application failures when updating to a new Entity Framework Core release.

IInfrastructure<IServiceProvider>.Instance

Gets the scoped IServiceProvider being used to resolve services.

This property is intended for use by extension methods that need to make use of services not directly exposed in the public API surface.

IResettableService.ResetState()

Resets the service so that it can be used from the pool.

IResettableService.ResetStateAsync(CancellationToken)

Extension Methods

GetCosmosClient(DatabaseFacade)

Gets the underlying CosmosClient for this DbContext.

GetCosmosDatabaseId(DatabaseFacade)

Gets the configured database name for this DbContext.

IsCosmos(DatabaseFacade)

Returns true if the database provider currently in use is the Cosmos provider.

GetInfrastructure<T>(IInfrastructure<T>)

Gets the value from a property that is being hidden using IInfrastructure<T>.

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

IsInMemory(DatabaseFacade)

Returns true if the database provider currently in use is the in-memory provider.

BeginTransaction(DatabaseFacade, IsolationLevel)

Starts a new transaction with a given IsolationLevel.

BeginTransactionAsync(DatabaseFacade, IsolationLevel, CancellationToken)

Asynchronously starts a new transaction with a given IsolationLevel.

CloseConnection(DatabaseFacade)

Closes the underlying DbConnection.

CloseConnectionAsync(DatabaseFacade)

Closes the underlying DbConnection.

ExecuteSql(DatabaseFacade, FormattableString)

Executes the given SQL against the database and returns the number of rows affected.

ExecuteSqlAsync(DatabaseFacade, FormattableString, CancellationToken)

Executes the given SQL against the database and returns the number of rows affected.

ExecuteSqlCommand(DatabaseFacade, RawSqlString, IEnumerable<Object>)
Obsolete.

Executes the given SQL against the database and returns the number of rows affected.

Note that this method does not start a transaction. To use this method with a transaction, first call BeginTransaction(DatabaseFacade, IsolationLevel) or UseTransaction(DatabaseFacade, DbTransaction).

Note that the current ExecutionStrategy is not used by this method since the SQL may not be idempotent and does not run in a transaction. An ExecutionStrategy can be used explicitly, making sure to also use a transaction if the SQL is not idempotent.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. You can also consider using ExecuteSqlInterpolated to use interpolated string syntax to create parameters.

ExecuteSqlCommand(DatabaseFacade, RawSqlString, Object[])
Obsolete.

Executes the given SQL against the database and returns the number of rows affected.

Note that this method does not start a transaction. To use this method with a transaction, first call BeginTransaction(DatabaseFacade, IsolationLevel) or UseTransaction(DatabaseFacade, DbTransaction).

Note that the current ExecutionStrategy is not used by this method since the SQL may not be idempotent and does not run in a transaction. An ExecutionStrategy can be used explicitly, making sure to also use a transaction if the SQL is not idempotent.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter - context.Database.ExecuteSqlCommand("SELECT * FROM [dbo].[SearchBlogs]({0})", userSuppliedSearchTerm). You can also consider using ExecuteSqlInterpolated to use interpolated string syntax to create parameters.

ExecuteSqlCommand(DatabaseFacade, FormattableString)
Obsolete.

Executes the given SQL against the database and returns the number of rows affected.

Note that this method does not start a transaction. To use this method with a transaction, first call BeginTransaction(DatabaseFacade, IsolationLevel) or UseTransaction(DatabaseFacade, DbTransaction).

Note that the current ExecutionStrategy is not used by this method since the SQL may not be idempotent and does not run in a transaction. An ExecutionStrategy can be used explicitly, making sure to also use a transaction if the SQL is not idempotent.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter - context.Database.ExecuteSqlCommand($"SELECT * FROM [dbo].[SearchBlogs]({userSuppliedSearchTerm})").

ExecuteSqlCommand(DatabaseFacade, String, Object[])
ExecuteSqlCommandAsync(DatabaseFacade, RawSqlString, IEnumerable<Object>, CancellationToken)
Obsolete.

Executes the given SQL against the database and returns the number of rows affected.

Note that this method does not start a transaction. To use this method with a transaction, first call BeginTransaction(DatabaseFacade, IsolationLevel) or UseTransaction(DatabaseFacade, DbTransaction).

Note that the current ExecutionStrategy is not used by this method since the SQL may not be idempotent and does not run in a transaction. An ExecutionStrategy can be used explicitly, making sure to also use a transaction if the SQL is not idempotent.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter. You can also consider using ExecuteSqlInterpolated to use interpolated string syntax to create parameters.

ExecuteSqlCommandAsync(DatabaseFacade, RawSqlString, Object[])
Obsolete.

Executes the given SQL against the database and returns the number of rows affected.

Note that this method does not start a transaction. To use this method with a transaction, first call BeginTransaction(DatabaseFacade, IsolationLevel) or UseTransaction(DatabaseFacade, DbTransaction).

Note that the current ExecutionStrategy is not used by this method since the SQL may not be idempotent and does not run in a transaction. An ExecutionStrategy can be used explicitly, making sure to also use a transaction if the SQL is not idempotent.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter - context.Database.ExecuteSqlCommandAsync("SELECT * FROM [dbo].[SearchBlogs]({0})", userSuppliedSearchTerm). You can also consider using ExecuteSqlInterpolated to use interpolated string syntax to create parameters.

ExecuteSqlCommandAsync(DatabaseFacade, RawSqlString, CancellationToken)
Obsolete.

Executes the given SQL against the database and returns the number of rows affected.

Note that this method does not start a transaction. To use this method with a transaction, first call BeginTransaction(DatabaseFacade, IsolationLevel) or UseTransaction(DatabaseFacade, DbTransaction).

Note that the current ExecutionStrategy is not used by this method since the SQL may not be idempotent and does not run in a transaction. An ExecutionStrategy can be used explicitly, making sure to also use a transaction if the SQL is not idempotent.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter - context.Database.ExecuteSqlCommandAsync("SELECT * FROM [dbo].[SearchBlogs]({0})", userSuppliedSearchTerm).

ExecuteSqlCommandAsync(DatabaseFacade, FormattableString, CancellationToken)
Obsolete.

Executes the given SQL against the database and returns the number of rows affected.

Note that this method does not start a transaction. To use this method with a transaction, first call BeginTransaction(DatabaseFacade, IsolationLevel) or UseTransaction(DatabaseFacade, DbTransaction).

Note that the current ExecutionStrategy is not used by this method since the SQL may not be idempotent and does not run in a transaction. An ExecutionStrategy can be used explicitly, making sure to also use a transaction if the SQL is not idempotent.

As with any API that accepts SQL it is important to parameterize any user input to protect against a SQL injection attack. You can include parameter place holders in the SQL query string and then supply parameter values as additional arguments. Any parameter values you supply will automatically be converted to a DbParameter - context.Database.ExecuteSqlCommandAsync($"SELECT * FROM [dbo].[SearchBlogs]({userSuppliedSearchTerm})").

ExecuteSqlCommandAsync(DatabaseFacade, String, CancellationToken, Object[])
ExecuteSqlInterpolated(DatabaseFacade, FormattableString)

Executes the given SQL against the database and returns the number of rows affected.

ExecuteSqlInterpolatedAsync(DatabaseFacade, FormattableString, CancellationToken)

Executes the given SQL against the database and returns the number of rows affected.

ExecuteSqlRaw(DatabaseFacade, String, IEnumerable<Object>)

Executes the given SQL against the database and returns the number of rows affected.

ExecuteSqlRaw(DatabaseFacade, String, Object[])

Executes the given SQL against the database and returns the number of rows affected.

ExecuteSqlRawAsync(DatabaseFacade, String, IEnumerable<Object>, CancellationToken)

Executes the given SQL against the database and returns the number of rows affected.

ExecuteSqlRawAsync(DatabaseFacade, String, Object[])

Executes the given SQL against the database and returns the number of rows affected.

ExecuteSqlRawAsync(DatabaseFacade, String, CancellationToken)

Executes the given SQL against the database and returns the number of rows affected.

GenerateCreateScript(DatabaseFacade)

Generates a script to create all tables for the current model.

GetAppliedMigrations(DatabaseFacade)

Gets all migrations that have been applied to the target database.

GetAppliedMigrationsAsync(DatabaseFacade, CancellationToken)

Asynchronously gets all migrations that have been applied to the target database.

GetCommandTimeout(DatabaseFacade)

Returns the timeout (in seconds) set for commands executed with this DbContext.

GetConnectionString(DatabaseFacade)

Gets the underlying connection string configured for this DbContext.

GetDbConnection(DatabaseFacade)

Gets the underlying ADO.NET DbConnection for this DbContext.

GetMigrations(DatabaseFacade)

Gets all the migrations that are defined in the configured migrations assembly.

GetPendingMigrations(DatabaseFacade)

Gets all migrations that are defined in the assembly but haven't been applied to the target database.

GetPendingMigrationsAsync(DatabaseFacade, CancellationToken)

Asynchronously gets all migrations that are defined in the assembly but haven't been applied to the target database.

HasPendingModelChanges(DatabaseFacade)

Returns true if the model has pending changes to be applied.

IsRelational(DatabaseFacade)

Returns true if the database provider currently in use is a relational database.

Migrate(DatabaseFacade, String)

Applies migrations for the context to the database. Will create the database if it does not already exist.

Migrate(DatabaseFacade)

Applies any pending migrations for the context to the database. Will create the database if it does not already exist.

MigrateAsync(DatabaseFacade, String, CancellationToken)

Asynchronously applies migrations for the context to the database. Will create the database if it does not already exist.

MigrateAsync(DatabaseFacade, CancellationToken)

Asynchronously applies any pending migrations for the context to the database. Will create the database if it does not already exist.

OpenConnection(DatabaseFacade)

Opens the underlying DbConnection.

OpenConnectionAsync(DatabaseFacade, CancellationToken)

Opens the underlying DbConnection.

SetCommandTimeout(DatabaseFacade, Nullable<Int32>)

Sets the timeout (in seconds) to use for commands executed with this DbContext.

SetCommandTimeout(DatabaseFacade, TimeSpan)

Sets the timeout to use for commands executed with this DbContext.

SetConnectionString(DatabaseFacade, String)

Sets the underlying connection string configured for this DbContext.

SetDbConnection(DatabaseFacade, DbConnection, Boolean)

Sets the underlying ADO.NET DbConnection for this DbContext.

SetDbConnection(DatabaseFacade, DbConnection)

Sets the underlying ADO.NET DbConnection for this DbContext.

SqlQuery<TResult>(DatabaseFacade, FormattableString)

Creates a LINQ query based on a raw SQL query, which returns a result set of a scalar type natively supported by the database provider.

SqlQueryRaw<TResult>(DatabaseFacade, String, Object[])

Creates a LINQ query based on a raw SQL query, which returns a result set of a scalar type natively supported by the database provider.

UseTransaction(DatabaseFacade, DbTransaction, Guid)

Sets the DbTransaction to be used by database operations on the DbContext.

UseTransaction(DatabaseFacade, DbTransaction)

Sets the DbTransaction to be used by database operations on the DbContext.

UseTransactionAsync(DatabaseFacade, DbTransaction, Guid, CancellationToken)

Sets the DbTransaction to be used by database operations on the DbContext.

UseTransactionAsync(DatabaseFacade, DbTransaction, CancellationToken)

Sets the DbTransaction to be used by database operations on the DbContext.

IsSqlite(DatabaseFacade)

Returns true if the database provider currently in use is the SQLite provider.

IsSqlServer(DatabaseFacade)

Returns true if the database provider currently in use is the SQL Server provider.

EnlistTransaction(DatabaseFacade, Transaction)

Specifies an existing Transaction to be used for database operations.

GetEnlistedTransaction(DatabaseFacade)

Returns the currently enlisted transaction.

Applies to