A library for handling Timestamps and SoftDelete as UTC DateTime or Unix in EntityFramework.
If you like or are using this project please give it a star. Thanks!
- Soft delete (DeletedAt).
- Timestamps (CreatedAt, UpdatedAt).
Both features support UTC DateTime and Unix Time Milliseconds format.
Example of Unix Time Milliseconds: currentmillis.com
Run this command to install
Install-Package Idam.EFTimestamps
or
dotnet add package Idam.EFTimestamps
-
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; } }
-
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; } }
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);
}
}
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();
}
}
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
.
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);
}
}
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 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.