Entity Framework Core
Last updated
Was this helpful?
Last updated
Was this helpful?
Structr.EntityFrameworkCore package provides methods to easily implement auto-auditing in DbContext
, ModelBuilder
extensions which applies the entity type default configuration and IQueryable<T>
extensions to paginate through a list of entities.
EntityFrameworkCore package is available on .
EntityFrameworkCore package have a reference to package that provides some abstract classes for entities, value objects and interfaces which are intended to help to implement .
Auto-auditing - it's entity change auditing for Entity Framework Core entities. The basic way to add auditing is to override the SaveChanges()
method of the DbContext
and plug in some logic for filling and tracking auditable properties (e.g. DateCreated
or CreatedBy
) automatically.
For example, create some entity that inherits Entity<TEntity,TKey>
class and ICreatable
interface.
Create your own DbContext
class implementation with configure some auditing dependencies (e.g. ITimestampProvider
) and override SaveChanges()
methods.
Now if you add a new instance of Issue
to DbSet<Issue>
and call SaveChanges()
or SaveChangesAsync()
method, the DateCreated
property will be filled in automatically.
Create your own DbContext
class implementation and override OnModelCreating()
method with calling ApplyEntityConfiguration()
, ApplyValueObjectConfiguration()
and/or ApplyAuditableConfiguration()
methods.
ModelBuilder
extension methods list:
ApplyEntityConfiguration
Applies the default configuration for all classes inherited from the Entity<TEntity, TKey>
. Automatically configures PK for entities with HasKey()
method.
ApplyValueObjectConfiguration
Applies the default configuration for all classes inherited from the ValueObject<TValueObject>
.
ApplyAuditableConfiguration
Applies the default configuration for all classes that implement the IAuditable
.
You can configure options when applying entity types default configurations. For example, configure PK column name for all domain entities:
EntityConfigurationOptions
properties:
Configure
Action<IMutableEntityType, EntityTypeBuilder>
Delegate for configuring Entities. Default value is null
.
Configure name strategy for all properties of value objects:
ValueObjectConfigurationOptions
properties:
Configure
Action<IMutableEntityType, string, OwnedNavigationBuilder>
Delegate for configuring Value Objects. Default value is null
.
Configure signed properties for auditable entities:
AuditableConfigurationOptions
properties:
SignedColumnMaxLength
int
Defines the maximum size of a signed column (CreatedBy
, ModifiedBy
, DeletedBy
). Default value is 50
.
SignedColumnIsRequired
bool
Defines if a signed column (CreatedBy
, ModifiedBy
, DeletedBy
) is required. Default value is false
.
Use .ToPagedList()
and .ToPagedListAsync()
extension methods for IQueryable<T>
to paginate result of entities query:
If you call ToPagedList()
method with pageSize
parameter equals -1
that returns all entities from DbContext
without limitation.