Nucleus .Net Core CMS

DbContext Class

Namespace: Nucleus.Data.EntityFramework
Assembly: Nucleus.Data.EntityFramework.dll
Entity-framework dbcontext implementation.

Remarks

This class includes automatic configuration of the data provider, automatic handling of Models.ModelBase audit properties, and a special SaveChanges overload which allows the caller to limit the scope of saved changes. Modules should inherit this class instead of inheriting Microsoft.EntityFrameworkCore.DbContext.

Constructor

Nucleus.Data.EntityFramework.DbContext Constructor

Nucleus.Data.EntityFramework.DbContext (DbContextConfigurator dbContextConfigurator, IHttpContextAccessor httpContextAccessor, ILoggerFactory loggerFactory)
Constructor.

Properties

LoggerFactory Property

Logger factory used to provide logger instance for entity-framework logs.

HttpContextAccessor Property

Http context accessor.
Remarks
This is used to determine the current user Id for populating audit columns.

ConfigureNucleusEntities Property

Get or set whether Nucleus entities should be configured in OnModelCreating.
Remarks
The default value is true. Data providers for business applications which reside in a different database to the Nucleus data objects can set this value to false in their constructor in order to prevent Nucleus entities from being added to the entity framework model for this context.

DbContextConfigurator Property

Methods

OnConfiguring (DbContextOptionsBuilder) Method

OnConfiguring (DbContextOptionsBuilder options)
Call the dbContextConfigurator to configure this DbContext.

OnModelCreating (ModelBuilder) Method

OnModelCreating (ModelBuilder builder)
Configure the Microsoft.EntityFrameworkCore.ModelBuilder with settings for the Core Nucleus entities.
Parameters
Name Type
builder Microsoft.EntityFrameworkCore.ModelBuilder

AttachClone<T> (<T>) Method

AttachClone<T> (<T> original)
Create a shallow copy of the original object and add it to the DbContext change tracker.
Type Parameters
T
Parameters
Name Type
original <T>

SaveChangesAsync (Boolean,CancellationToken) Method

SaveChangesAsync (Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
Save changes made in this context, after updating audit properties.
Parameters
Name Type
acceptAllChangesOnSuccess Boolean
cancellationToken System.Threading.CancellationToken

SaveChanges

SaveChanges ()
Save changes made in this context, after updating audit properties.

SaveChanges (Boolean) Method

SaveChanges (Boolean acceptAllChangesOnSuccess)
Save changes made in this context, after updating audit properties.
Parameters
Name Type
acceptAllChangesOnSuccess Boolean

SaveChangesAsync<TEntity>

SaveChangesAsync<TEntity> ()
Save changes made in this context for the type specified by TEntity ONLY, after updating audit properties.
Type Parameters
TEntity

SaveChanges<TEntity>

SaveChanges<TEntity> ()
Save changes made in this context for the type specified by TEntity ONLY, after updating audit properties.
Type Parameters
TEntity

ManageSpecialColumns

ManageSpecialColumns ()
Automatically set properties of entity framework entries before adding or updating the database.
Remarks
Extensions can override this class to perform their own automatic actions.

CurrentUserId

CurrentUserId ()
Retrieve the currently logged-on user's Id