K.Extensions.EfCore
1.0.0
dotnet add package K.Extensions.EfCore --version 1.0.0
NuGet\Install-Package K.Extensions.EfCore -Version 1.0.0
<PackageReference Include="K.Extensions.EfCore" Version="1.0.0" />
paket add K.Extensions.EfCore --version 1.0.0
#r "nuget: K.Extensions.EfCore, 1.0.0"
// Install K.Extensions.EfCore as a Cake Addin #addin nuget:?package=K.Extensions.EfCore&version=1.0.0 // Install K.Extensions.EfCore as a Cake Tool #tool nuget:?package=K.Extensions.EfCore&version=1.0.0
EfCore Extension
K.Extensions.EfCore
is a .NET library that enables you to implement interfaces for your entities to support soft deletes and other functionalities.
Introduction
Since some of these actions are frequently used repeatedly, this extension library was created.
This library provides extended functionalities in conjunction with EntityFramework. It includes mechanisms for automatic logging of creation and modification times as well as soft deleting of entities.
How It Works
First, add the package.
Implementing the Interfaces
To use the extended functions, your entity classes should implement the corresponding interfaces.
using System;
using K.Extensions.Datetime.Models;
public class MyEntity : IAmAuditAble, IAmSoftdeleteAble, IHaveCreationTime, IHaveModificationTime
{
public int Id { get; set; }
public DateTime CreationTime { get; private set; }
public DateTime? ModificationTime { get; private set; }
public DateTime? DeletionTime { get; private set; }
public bool IsDeleted { get; private set; }
}
Configuring the DbContext
Extend your DbContext from BaseDbContext to enable audit and timestamp functionality.
using K.Extensions.Datetime;
using Microsoft.EntityFrameworkCore;
public class MyDbContext : BaseDbContext
{
public DbSet<MyEntity> MyEntities { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfigurationsFromAssembly(typeof(MyDbContext).Assembly);
}
}
Using BaseDbContext also creates audit logging in the database. The audit logs include:
public Guid Id { get; set; }
public string EntityName { get; set; }
public string EntityId { get; set; }
public string Operation { get; set; }
public string Changes { get; set; }
public DateTime Timestamp { get; set; }
There is also the option to use only the interceptors in your DbContext. For this, the corresponding interceptors should be included in your dependency injection setup.
var sp = new ServiceCollection()
.AddSingleton<SoftDeleteInterceptor>()
.AddSingleton<ModificationTimeInterceptor>()
.AddSingleton<CreationTimeInterceptor>()
.BuildServiceProvider();
var options = new DbContextOptionsBuilder<InterceptorDbContext>()
....
.AddInterceptors(
sp.GetRequiredService<SoftDeleteInterceptor>(),
sp.GetRequiredService<ModificationTimeInterceptor>(),
sp.GetRequiredService<CreationTimeInterceptor>()
)
...
When using the interceptors without BaseDbContext, audit logging is not included. For this, you might need to implement your own audit logging mechanism.
Using the Interceptors
The interceptors are automatically applied when you create or update your entities in the database.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Microsoft.EntityFrameworkCore (>= 8.0.6)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.0 | 101 | 7/5/2024 |