Idam.EFTimestamps
8.0.0
dotnet add package Idam.EFTimestamps --version 8.0.0
NuGet\Install-Package Idam.EFTimestamps -Version 8.0.0
<PackageReference Include="Idam.EFTimestamps" Version="8.0.0" />
paket add Idam.EFTimestamps --version 8.0.0
#r "nuget: Idam.EFTimestamps, 8.0.0"
// Install Idam.EFTimestamps as a Cake Addin #addin nuget:?package=Idam.EFTimestamps&version=8.0.0 // Install Idam.EFTimestamps as a Cake Tool #tool nuget:?package=Idam.EFTimestamps&version=8.0.0
Idam.EFTimestamps
A library for handling Timestamps and SoftDelete as UTC DateTime or Unix in EntityFramework..
Give a Star! ⭐
If you like or are using this project please give it a star. Thanks!
Features
- Soft delete (DeletedAt).
- Timestamps (CreatedAt, UpdatedAt).
Both features support UTC DateTime and Unix Time Milliseconds format.
Example of Unix Time Milliseconds: currentmillis.com
Get started
Run this command to install
Install-Package Idam.EFTimestamps
or
dotnet tool install Idam.EFTimestamps
Usage
Using Timestamps
Add
AddTimestamps()
in your context.using Idam.EFTimestamps.Extensions; public class MyDbContext : DbContext { public override int SaveChanges(bool acceptAllChangesOnSuccess) { ChangeTracker.AddTimestamps(); return base.SaveChanges(acceptAllChangesOnSuccess); } public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default) { ChangeTracker.AddTimestamps(); return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); } }
Implement an Interface (
ITimeStamps
orITimeStampsUnix
) to your entity.using Idam.EFTimestamps.Interfaces; /// BaseEntity public abstract class BaseEntity { public int Id { get; set; } public string Name { get; set; } = default!; public string? Description { get; set; } } /// Using UTC DateTime Format public class Dt : BaseEntity, ITimeStamps { public DateTime CreatedAt { get; set; } public DateTime UpdatedAt { get; set; } } /// Using Unix Format public class Unix : ITimeStampsUnix { public long CreatedAt { get; set; } public long UpdatedAt { get; set; } }
Using SoftDelete
Add
AddTimestamps()
andAddSoftDeleteFilter()
in your context.using Idam.EFTimestamps.Extensions; public class MyDbContext : DbContext { public override int SaveChanges(bool acceptAllChangesOnSuccess) { ChangeTracker.AddTimestamps(); return base.SaveChanges(acceptAllChangesOnSuccess); } public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default) { ChangeTracker.AddTimestamps(); return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken); } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.AddSoftDeleteFilter(); base.OnModelCreating(modelBuilder); } }
Implement an Interface (
ISoftDelete
orISoftDeleteUnix
) to your entity.using Idam.EFTimestamps.Interfaces; /// Using UTC DateTime Format public class Dt : BaseEntity, ISoftDelete { public DateTime? DeletedAt { get; set; } } /// Using Unix Format public class Unix : BaseEntity, ISoftDeleteUnix { public long? DeletedAt { get; set; } }
Restore
The SoftDelete has a Restore()
function, so you can restore the deleted data.
using Idam.EFTimestamps.Extensions;
/// Your context
public class MyDbContext : DbContext
{
public DbSet<Dt> Dts { get; set; }
}
/// Dt Controller
public class DtController
{
readonly MyDbContext _context;
public async Task<IActionResult> RestoreAsync(Dt dt)
{
var restored = _context.Dts.Restore(dt);
await context.SaveChangesAsync();
return Ok(restored);
}
}
Force Remove
The SoftDelete has a ForceRemove()
function, so you can permanently remove the data.
/// Dt Controller
public class DtController
{
readonly MyDbContext _context;
public async Task<IActionResult> ForceRemoveAsync(Dt dt)
{
_context.Dts.ForceRemove(dt);
await context.SaveChangesAsync();
return Ok();
}
}
Trashed
The SoftDelete has a Trashed()
function to check if current data is deleted.
/// Dt Controller
public class DtController
{
public IActionResult IsDeleted(Dt dt)
{
bool isDeleted = dt.Trashed();
return Ok(isDeleted);
}
}
The
Trashed()
function only shows when your entity implements an interfaceISoftDelete
orISoftDeleteUnix
.
Ignore global softdelete filter
By default the deleted data filtered from the query, if you want to get the deleted data you can ignore the global softdelete filter by using IgnoreQueryFilters()
.
/// Dt Controller
public class DtController
{
readonly MyDbContext _context;
public async Task<IActionResult> GetAllDeletedAsync()
{
var deleteds = await _context.Dts
.IgnoreQueryFilters()
.Where(x => x.DeletedAt != null)
.ToListAsync();
return Ok(deleteds);
}
}
Using Custom TimeStamps fields
By default, the TimeStamps interface uses CreatedAt, UpdatedAt, and DeletedAt as field names. To customize the TimeStamps fields simplify just add ColumnAttribute to the fields.
Migrating
Migrating from 7.0.0
- Remove [TimeStampsAttribute], [TimeStampsUnixAttribute], and [TimeStampsUtcAttribute].
- If you use custom field from TimeStampsAttribute, then add the ColumnAttribute to each TimeStamps fields.
- Remove/Create your own IGuidEntity.
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 |
---|---|---|
8.0.0 | 104 | 6/26/2024 |
- Update to .net 8.
- Update Microsoft.EntityFrameworkCore to 8.0.6.
- Removed TimeStampsAttribute (use ColumnAttribute instead).
- Removed IGuidEntity.