DatabaseFacade 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 : IInfrastructure<IServiceProvider>
Public Class DatabaseFacade
    Implements IInfrastructure(Of IServiceProvider)
Inheritance
System.ObjectSystem.Object
DatabaseFacadeDatabaseFacade
Implements
IInfrastructure<System.IServiceProvider>IInfrastructure(Of System.IServiceProvider)

Constructors

DatabaseFacade(DbContext) 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.

public DatabaseFacade(DbContext context)
Public Sub New(context As DbContext)
Parameters
context
DbContext DbContext

The context this database API belongs to .

Properties

AutoTransactionsEnabled AutoTransactionsEnabled

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.

Setting this value to false will also disable the IExecutionStrategy for SaveChanges()

The default value is true, meaning that SaveChanges will always use a transaction when saving changes.

Setting this value to false should only be done with caution since the database could be left in a corrupted state if SaveChanges fails.

public virtual bool AutoTransactionsEnabled { get; set; }
Public Overridable Property AutoTransactionsEnabled As Boolean
Value
System.Boolean System.Boolean

CurrentTransaction CurrentTransaction

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

This property will be null unless one of the 'BeginTransaction' or 'UseTransaction' methods has been called, some of which are available as extension methods installed by EF providers. No attempt is made to obtain a transaction from the current DbConnection or similar.

For relational databases, the underlying DbTransaction can be obtained using the 'Microsoft.EntityFrameworkCore.Storage.GetDbTransaction'extension method on the returned IDbContextTransaction.

public virtual IDbContextTransaction CurrentTransaction { get; }
Public Overridable ReadOnly Property CurrentTransaction As IDbContextTransaction

Methods

BeginTransaction() BeginTransaction()

Starts a new transaction.

public virtual IDbContextTransaction BeginTransaction()
Public Overridable Function BeginTransaction As IDbContextTransaction
Returns

A IDbContextTransaction that represents the started transaction.

BeginTransactionAsync(CancellationToken) BeginTransactionAsync(CancellationToken)

Asynchronously starts a new transaction.

public virtual Task<IDbContextTransaction> BeginTransactionAsync(CancellationToken cancellationToken = null)
Public Overridable Function BeginTransactionAsync(cancellationToken As CancellationToken = Nothing) As Task(Of IDbContextTransaction)
Parameters
cancellationToken
System.Threading.CancellationToken System.Threading.CancellationToken

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

Returns
System.Threading.Tasks.Task<IDbContextTransaction> System.Threading.Tasks.Task(Of IDbContextTransaction)

A task that represents the asynchronous transaction initialization. The task result contains a IDbContextTransaction that represents the started transaction.

CommitTransaction() CommitTransaction()

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

public virtual void CommitTransaction()
Public Overridable Sub CommitTransaction

CreateExecutionStrategy() CreateExecutionStrategy()

Creates an instance of the configured IExecutionStrategy.

public virtual IExecutionStrategy CreateExecutionStrategy()
Public Overridable Function CreateExecutionStrategy As IExecutionStrategy
Returns

EnsureCreated() EnsureCreated()

Ensures that the database for the context exists. If it exists, no action is taken. If it does not exist then the database and all its schema are created. If the database exists, then no effort is made to ensure it is compatible with the model for this context.

Note that this API does not use migrations to create the database. In addition, the database that is created cannot be later updated using migrations. If you are targeting a relational database and using migrations, you can use the DbContext.Database.Migrate() method to ensure the database is created and all migrations are applied.

public virtual bool EnsureCreated()
Public Overridable Function EnsureCreated As Boolean
Returns
System.Boolean System.Boolean

True if the database is created, false if it already existed.

EnsureCreatedAsync(CancellationToken) EnsureCreatedAsync(CancellationToken)

Asynchronously ensures that the database for the context exists. If it exists, no action is taken. If it does not exist then the database and all its schema are created. If the database exists, then no effort is made to ensure it is compatible with the model for this context.

Note that this API does not use migrations to create the database. In addition, the database that is created cannot be later updated using migrations. If you are targeting a relational database and using migrations, you can use the DbContext.Database.Migrate() method to ensure the database is created and all migrations are applied.

public virtual Task<bool> EnsureCreatedAsync(CancellationToken cancellationToken = null)
Public Overridable Function EnsureCreatedAsync(cancellationToken As CancellationToken = Nothing) As Task(Of Boolean)
Parameters
cancellationToken
System.Threading.CancellationToken System.Threading.CancellationToken

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

Returns
System.Threading.Tasks.Task<System.Boolean> System.Threading.Tasks.Task(Of System.Boolean)

A task that represents the asynchronous save operation. The task result contains true if the database is created, false if it already existed.

EnsureDeleted() 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 an no effort is made to remove just the database objects that are used by the model for this context.

public virtual bool EnsureDeleted()
Public Overridable Function EnsureDeleted As Boolean
Returns
System.Boolean System.Boolean

True if the database is deleted, false if it did not exist.

EnsureDeletedAsync(CancellationToken) 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 an no effort is made to remove just the database objects that are used by the model for this context.

public virtual Task<bool> EnsureDeletedAsync(CancellationToken cancellationToken = null)
Public Overridable Function EnsureDeletedAsync(cancellationToken As CancellationToken = Nothing) As Task(Of Boolean)
Parameters
cancellationToken
System.Threading.CancellationToken System.Threading.CancellationToken

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

Returns
System.Threading.Tasks.Task<System.Boolean> System.Threading.Tasks.Task(Of System.Boolean)

A task that represents the asynchronous save operation. The task result contains true if the database is deleted, false if it did not exist.

Equals(Object) Equals(Object)

Inherited from System.Object

Equals(Object, Object) Equals(Object, Object)

Inherited from System.Object

GetHashCode() GetHashCode()

Inherited from System.Object

GetType() GetType()

Inherited from System.Object

MemberwiseClone() MemberwiseClone()

Inherited from System.Object

ReferenceEquals(Object, Object) ReferenceEquals(Object, Object)

Inherited from System.Object

RollbackTransaction() RollbackTransaction()

Discards the outstanding operations in the current transaction.

public virtual void RollbackTransaction()
Public Overridable Sub RollbackTransaction

ToString() ToString()

Inherited from System.Object

Explicit Interface Implementations

IInfrastructure.Instance Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure.Instance

Gets the scoped System.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.

IServiceProvider IInfrastructure<IServiceProvider>.Instance { get; }
ReadOnly WriteOnly Property Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.Instance As IServiceProvider Implements IInfrastructure(Of IServiceProvider).Instance
Returns
System.IServiceProvider System.IServiceProvider