Table of Contents

Class ApplicationDbContext

Namespace
ProjectTemplate.Infrastructure.Data
Assembly
ProjectTemplate.Infrastructure.dll

Represents the EF Core database context for the ProjectTemplate application.

public sealed class ApplicationDbContext : DbContext, IInfrastructure<IServiceProvider>, IDbContextDependencies, IDbSetCache, IDbContextPoolable, IResettableService, IDisposable, IAsyncDisposable
Inheritance
ApplicationDbContext
Implements
Inherited Members

Constructors

ApplicationDbContext(DbContextOptions<ApplicationDbContext>, ILogger<ApplicationDbContext>, IApplicationSaveChangesPipeline, ApplicationSaveChangesInterceptor?)

Represents the EF Core database context for the ProjectTemplate application.

public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options, ILogger<ApplicationDbContext> logger, IApplicationSaveChangesPipeline saveChangesPipeline, ApplicationSaveChangesInterceptor? saveChangesInterceptor = null)

Parameters

options DbContextOptions<ApplicationDbContext>
logger ILogger<ApplicationDbContext>
saveChangesPipeline IApplicationSaveChangesPipeline
saveChangesInterceptor ApplicationSaveChangesInterceptor

Properties

ApplicationAuditCompletionOutboxEntries

Gets the durable, minimized audit-completion outbox entries.

public DbSet<ApplicationAuditCompletionOutboxEntry> ApplicationAuditCompletionOutboxEntries { get; }

Property Value

DbSet<ApplicationAuditCompletionOutboxEntry>

ApplicationAuditReconciliationFindings

public DbSet<ApplicationAuditReconciliationFinding> ApplicationAuditReconciliationFindings { get; }

Property Value

DbSet<ApplicationAuditReconciliationFinding>

ApplicationAuditReconciliationRemediations

public DbSet<ApplicationAuditReconciliationRemediation> ApplicationAuditReconciliationRemediations { get; }

Property Value

DbSet<ApplicationAuditReconciliationRemediation>

AuditRecords

Gets the audit records for the application.

public DbSet<AuditRecord> AuditRecords { get; }

Property Value

DbSet<AuditRecord>

ExternalLoginAccounts

Gets the external login account links for the application.

public DbSet<ExternalLoginAccount> ExternalLoginAccounts { get; }

Property Value

DbSet<ExternalLoginAccount>

Methods

HasUnsavedChanges()

public bool HasUnsavedChanges()

Returns

bool

OnConfiguring(DbContextOptionsBuilder)

Override this method to configure the database (and other options) to be used for this context. This method is called for each instance of the context that is created. The base implementation does nothing.

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)

Parameters

optionsBuilder DbContextOptionsBuilder

A builder used to create or modify options for this context. Databases (and other extensions) typically define extension methods on this object that allow you to configure the context.

Remarks

In situations where an instance of DbContextOptions may or may not have been passed to the constructor, you can use IsConfigured to determine if the options have already been set, and skip some or all of the logic in OnConfiguring(DbContextOptionsBuilder).

See DbContext lifetime, configuration, and initialization for more information and examples.

OnModelCreating(ModelBuilder)

Override this method to further configure the model that was discovered by convention from the entity types exposed in DbSet<TEntity> properties on your derived context. The resulting model may be cached and re-used for subsequent instances of your derived context.

protected override void OnModelCreating(ModelBuilder modelBuilder)

Parameters

modelBuilder ModelBuilder

The builder being used to construct the model for this context. Databases (and other extensions) typically define extension methods on this object that allow you to configure aspects of the model that are specific to a given database.

Remarks

If a model is explicitly set on the options for this context (via UseModel(IModel)) then this method will not be run. However, it will still run when creating a compiled model.

See Modeling entity types and relationships for more information and examples.

SaveChanges()

Saves all changes made in this context to the database.

public override int SaveChanges()

Returns

int

The number of state entries written to the database.

Remarks

This method will automatically call DetectChanges() to discover any changes to entity instances before saving to the underlying database. This can be disabled via AutoDetectChangesEnabled.

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See Saving data in EF Core for more information and examples.

Exceptions

DbUpdateException

An error is encountered while saving to the database.

DbUpdateConcurrencyException

A concurrency violation is encountered while saving to the database. A concurrency violation occurs when an unexpected number of rows are affected during save. This is usually because the data in the database has been modified since it was loaded into memory.

SaveChanges(bool)

Saves all changes made in this context to the database.

public override int SaveChanges(bool acceptAllChangesOnSuccess = true)

Parameters

acceptAllChangesOnSuccess bool

Indicates whether AcceptAllChanges() is called after the changes have been sent successfully to the database.

Returns

int

The number of state entries written to the database.

Remarks

This method will automatically call DetectChanges() to discover any changes to entity instances before saving to the underlying database. This can be disabled via AutoDetectChangesEnabled.

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See Saving data in EF Core for more information and examples.

Exceptions

DbUpdateException

An error is encountered while saving to the database.

DbUpdateConcurrencyException

A concurrency violation is encountered while saving to the database. A concurrency violation occurs when an unexpected number of rows are affected during save. This is usually because the data in the database has been modified since it was loaded into memory.

SaveChangesAsync(bool, CancellationToken)

Saves all changes made in this context to the database.

public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)

Parameters

acceptAllChangesOnSuccess bool

Indicates whether AcceptAllChanges() is called after the changes have been sent successfully to the database.

cancellationToken CancellationToken

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

Returns

Task<int>

A task that represents the asynchronous save operation. The task result contains the number of state entries written to the database.

Remarks

This method will automatically call DetectChanges() to discover any changes to entity instances before saving to the underlying database. This can be disabled via AutoDetectChangesEnabled.

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See Saving data in EF Core for more information and examples.

Exceptions

DbUpdateException

An error is encountered while saving to the database.

DbUpdateConcurrencyException

A concurrency violation is encountered while saving to the database. A concurrency violation occurs when an unexpected number of rows are affected during save. This is usually because the data in the database has been modified since it was loaded into memory.

OperationCanceledException

If the CancellationToken is canceled.

SaveChangesAsync(CancellationToken)

Saves all changes made in this context to the database.

public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)

Parameters

cancellationToken CancellationToken

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

Returns

Task<int>

A task that represents the asynchronous save operation. The task result contains the number of state entries written to the database.

Remarks

This method will automatically call DetectChanges() to discover any changes to entity instances before saving to the underlying database. This can be disabled via AutoDetectChangesEnabled.

Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance. This includes both parallel execution of async queries and any explicit concurrent use from multiple threads. Therefore, always await async calls immediately, or use separate DbContext instances for operations that execute in parallel. See Avoiding DbContext threading issues for more information and examples.

See Saving data in EF Core for more information and examples.

Exceptions

DbUpdateException

An error is encountered while saving to the database.

DbUpdateConcurrencyException

A concurrency violation is encountered while saving to the database. A concurrency violation occurs when an unexpected number of rows are affected during save. This is usually because the data in the database has been modified since it was loaded into memory.

OperationCanceledException

If the CancellationToken is canceled.