DatabaseFacade DatabaseFacade Class

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.

Syntax

Declaration

public class DatabaseFacade : IInfrastructure<IServiceProvider>Public Class DatabaseFacade
    Implements IInfrastructure(Of IServiceProvider)

Inheritance Hierarchy

  • System.Object
    System.Object
  • DatabaseFacade
    DatabaseFacade

Inherited Members

ToString(),
ToString(),
Equals(Object),
Equals(Object),
Equals(Object, Object),
Equals(Object, Object),
ReferenceEquals(Object, Object),
ReferenceEquals(Object, Object),
GetHashCode(),
GetHashCode(),
GetType(),
GetType(),
MemberwiseClone()
MemberwiseClone()

Constructors summary

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 summary

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.

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.

Methods summary

Starts a new transaction.

Asynchronously starts a new transaction.

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

Creates an instance of the configured IExecutionStrategy.

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.

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.

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.

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.

Discards the outstanding operations in the current transaction.

Explicit Interface Implementations summary

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.

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

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

    Property 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

    Property Value

Methods

  • BeginTransaction()
    BeginTransaction()

    Starts a new transaction.

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

    Returns

  • 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

  • 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<TResult><System.Boolean>
      System.Threading.Tasks.Task<TResult>(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<TResult><System.Boolean>
      System.Threading.Tasks.Task<TResult>(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.

  • RollbackTransaction()
    RollbackTransaction()

    Discards the outstanding operations in the current transaction.

    public virtual void RollbackTransaction()Public Overridable Sub RollbackTransaction

Explicit Interface Implementations

  • IInfrastructure<IServiceProvider>.Instance
    Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure<System.IServiceProvider>.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

Details

Assembly

Microsoft.EntityFrameworkCore.dll