Idam.EFTimestamps 8.0.0

dotnet add package Idam.EFTimestamps --version 8.0.0                
NuGet\Install-Package Idam.EFTimestamps -Version 8.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Idam.EFTimestamps" Version="8.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Idam.EFTimestamps --version 8.0.0                
#r "nuget: Idam.EFTimestamps, 8.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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

NuGet .NET

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

  1. 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);
        }
    }
    
  2. Implement an Interface (ITimeStamps or ITimeStampsUnix) 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

  1. Add AddTimestamps() and AddSoftDeleteFilter() 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);
        }
    }
    
  2. Implement an Interface (ISoftDelete or ISoftDeleteUnix) 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 interface ISoftDelete or ISoftDeleteUnix.

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

  1. Remove [TimeStampsAttribute], [TimeStampsUnixAttribute], and [TimeStampsUtcAttribute].
  2. If you use custom field from TimeStampsAttribute, then add the ColumnAttribute to each TimeStamps fields.
  3. Remove/Create your own IGuidEntity.
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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 103 6/26/2024

- Update to .net 8.
- Update Microsoft.EntityFrameworkCore to 8.0.6.
- Removed TimeStampsAttribute (use ColumnAttribute instead).
- Removed IGuidEntity.