Table of Contents

Class ApplicationDbContext

Namespace
KYS.EFCore.Library.DBContext
Assembly
KYS.EFCore.Library.dll

Base DbContext that provides an ActionLog table and audit-aware saving through SaveChangesWithAuditAsync(Guid?, Formatting?).

Derive from this class to add your own DbSet<TEntity> properties.

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

Remarks

The ActionLog mapping is provider-aware: when the database provider name contains Npgsql, snake_case table/column/index names and PostgreSQL column types are used; otherwise PascalCase names and provider-default column types are used (e.g. for SQLite in tests).

Constructors

ApplicationDbContext(DbContextOptions)

Initializes a new instance of the ApplicationDbContext class.

public ApplicationDbContext(DbContextOptions options)

Parameters

options DbContextOptions

The options to be used by the DbContext.

Properties

ActionLog

Gets or sets the audit records written by SaveChangesWithAuditAsync(Guid?, Formatting?).

public virtual DbSet<ActionLog> ActionLog { get; set; }

Property Value

DbSet<ActionLog>

Methods

OnModelCreating(ModelBuilder)

Configures the ActionLog entity mapping (table, index, column names and types) based on the current database provider.

protected override void OnModelCreating(ModelBuilder modelBuilder)

Parameters

modelBuilder ModelBuilder

The builder used to construct the model for this context.

Remarks

Derived contexts that override this method should call base.OnModelCreating(modelBuilder) so that the ActionLog mapping is kept.

SaveChangesWithAuditAsync(Guid?, Formatting?)

Saves all changes made in this context to the database and writes an ActionLog record for every added, modified or deleted entity.

public Task<int> SaveChangesWithAuditAsync(Guid? actionBy = null, FormattingHelper.Formatting? formatting = null)

Parameters

actionBy Guid?

The identifier of the user making the change, stored in CreatedBy.

formatting FormattingHelper.Formatting?

The naming convention applied to ReferenceTable and ColumnName. When it is KYS.Library.Helpers.FormattingHelper.Formatting.SnakeCase, the keys of RecordValue are converted to snake_case as well. null keeps the original names.

Returns

Task<int>

The number of state entries written to the database by the main save, including the audit records that could be created before saving.

Remarks

  1. Changes are detected and an AuditEntry is built for each tracked entity (excluding ActionLog entities and detached or unchanged entries).
  2. Audit records whose values are already known are added, then the changes are saved.
  3. Audit records for entities with database-generated values (e.g. identity keys) are completed with the generated values and saved with a second SaveChangesAsync(CancellationToken) call.

The two saves are not wrapped in a transaction by this method. Use an IUnitOfWork (or an explicit transaction) if both must succeed or fail together.

Exceptions

InvalidOperationException

An audit entry could not be converted to an ActionLog, for example because a name could not be formatted.