Idam.EFTimestamps 8.1.1

dotnet add package Idam.EFTimestamps --version 8.1.1                
NuGet\Install-Package Idam.EFTimestamps -Version 8.1.1                
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.1.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Idam.EFTimestamps --version 8.1.1                
#r "nuget: Idam.EFTimestamps, 8.1.1"                
#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.1.1

// Install Idam.EFTimestamps as a Cake Tool
#tool nuget:?package=Idam.EFTimestamps&version=8.1.1                

Idam.EFTimestamps

NuGet .NET

A lightweight .NET library that simplifies timestamp management in Entity Framework Core by automatically handling creation, update, and deletion timestamps. The library supports multiple timestamp formats including DateTime, UTC DateTime, and Unix Time Milliseconds.

⭐ Support

If you find this library helpful, please consider giving it a star! Your support helps make it better.

🚀 Features

  • Automatic management of entity timestamps (Created, Updated, Deleted).
  • Multiple timestamp format support:
    • DateTime.
    • UTC DateTime.
    • Unix Time Milliseconds (learn more).
  • Built-in soft delete functionality.
  • Seamless integration with existing EF Core projects.
  • Customizable timestamp field names.
  • Individual timestamp interfaces for flexibility.

📦 Installation

Using Package Manager Console:

Install-Package Idam.EFTimestamps

Using .NET CLI

dotnet add package Idam.EFTimestamps

🔧 Basic Setup

1. Configure DbContext

Add AddTimestamps() in your DbContext.

...
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. Define your entity

Choose the appropriate interface based on your timestamp format needs (ITimeStamps or ITimeStampsUtc or ITimeStampsUnix).

using Idam.EFTimestamps.Interfaces;

/// Local DateTime
public class Product : ITimeStamps
{
    ...
    public DateTime CreatedAt { get; set; }
    public DateTime UpdatedAt { get; set; }
}

/// UTC DateTime
public class Product : ITimeStampsUtc
{
    ...
    public DateTime CreatedAt { get; set; }
    public DateTime UpdatedAt { get; set; }
}

/// Unix Time
public class Product : ITimeStampsUnix
{
    ...
    public long CreatedAt { get; set; }
    public long UpdatedAt { get; set; }
}

🗑️ Soft Delete Feature

Setup

  1. Update your DbContext

    Add AddSoftDeleteFilter() in your context.

    using Idam.EFTimestamps.Extensions;
    
    public class MyDbContext : DbContext
    {
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.AddSoftDeleteFilter();
    
            base.OnModelCreating(modelBuilder);
        }
    }
    
  2. Implement soft delete in your entities

    Choose the appropriate interface based on your timestamp format needs (ISoftDelete or ISoftDeleteUtc or ISoftDeleteUnix).

    using Idam.EFTimestamps.Interfaces;
    
    /// Local DateTime
    public class Product : ISoftDelete
    {
        ...
        public DateTime? DeletedAt { get; set; }
    }
    
    /// UTC DateTime
    public class Product : ISoftDeleteUtc
    {
        ...
        public DateTime? DeletedAt { get; set; }
    }
    
    /// Unix Time
    public class Product : ISoftDeleteUnix
    {
        ...
        public long? DeletedAt { get; set; }
    }
    

Soft Delete Operations

// Restore a soft-deleted item
_context.Products.Restore(product);
await context.SaveChangesAsync();

// Permanently delete an item
_context.Products.ForceRemove(product);
await context.SaveChangesAsync();

// Check if item is deleted
bool isDeleted = product.Trashed();

// Query including soft-deleted items
var deletedProducts = await _context.Products
    .IgnoreQueryFilters()
    .Where(x => x.DeletedAt != null)
    .ToListAsync();

🎨 Customization

Custom Field Names

public class Product : ITimeStamps, ISoftDelete
{
    [Column("AddedAt")]
    public DateTime CreatedAt { get; set; }
    
    [Column("ModifiedAt")]
    public DateTime UpdatedAt { get; set; }
    
    [Column("RemovedAt")]
    public DateTime? DeletedAt { get; set; }
}

Individual Timestamp Interfaces

public class Product : ICreatedAt { }      // Only creation time
public class Product : ICreatedAtUtc { }   // Only creation time (UTC)
public class Product : ICreatedAtUnix { }  // Only creation time (Unix time)

public class Product : IUpdatedAt { }      // Only update time
public class Product : IUpdatedAtUtc { }   // Only update time (UTC)
public class Product : IUpdatedAtUnix { }  // Only update time (Unix time)

public class Product : ISoftDelete { }     // Only soft delete
public class Product : ISoftDeleteUtc { }  // Only soft delete (UTC)
public class Product : ISoftDeleteUnix { } // Only soft delete (Unix time)

🔄 Migration Guide

When upgrading from version 8.0.0:

  • Replace ITimeStamps with ITimeStampsUtc.
  • Replace ISoftDelete with ISoftDeleteUtc.
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.  net9.0 is compatible. 
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.1.1 78 12/29/2024
8.1.0 84 12/29/2024
8.0.0 117 6/26/2024

Add net9 target framework.