EFTimestamps.Annotations 1.1.0

dotnet add package EFTimestamps.Annotations --version 1.1.0
NuGet\Install-Package EFTimestamps.Annotations -Version 1.1.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="EFTimestamps.Annotations" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EFTimestamps.Annotations --version 1.1.0
#r "nuget: EFTimestamps.Annotations, 1.1.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 EFTimestamps.Annotations as a Cake Addin
#addin nuget:?package=EFTimestamps.Annotations&version=1.1.0

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

EFTimestamps

NuGet version Downloads Build Status codecov License

Put timestamps in your entities the easy way

Motivation

Using timestamps is something I do in almost every app I make, if you're here probably you do too. Since I always follow the same approach to implement them I just ended up making this library in order not to have to rewrite this code ever again. Hopefully you'll be able to find some use for it too!

Quick start

Install both EFTimestamps.Annotations and EFTimestamps.Configuration. It is divided in two libs so that you can have your entities in a separate dll that does not know about EFCore.

After that, mark your timestamp properties with the data annotations.

using EFTimestamps.Annotations;

public class TestEntity
{
    [CreatedAt]
    public DateTime CreatedAt { get; set; }

    [UpdatedAt]
    public DateTime UpdatedAt { get; set; }
}

Then, go to your DbContext and override the variant of SaveChanges or SaveChangesAsync that you use in your persistence layer.

Additionally, you can tell EFCore to create indexes for your timestamps by calling modelBuilder.IndexTimestamps() in OnModelCreating.

Here is an example of a DbContext that does both these things.

using EFTimestamps.Configuration;
using Microsoft.EntityFrameworkCore;

public class TestContext : DbContext
{
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.IndexTimestamps();
    }

    public override int SaveChanges()
    {
        this.UpdateTimestamps();
        return base.SaveChanges();
    }

    public override int SaveChanges(bool acceptAllChangesOnSuccess)
    {
        this.UpdateTimestamps();
        return base.SaveChanges(acceptAllChangesOnSuccess);
    }

    public override Task<int> SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
    {
        this.UpdateTimestamps();
        return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
    }

    public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = default)
    {
        this.UpdateTimestamps();
        return base.SaveChangesAsync(cancellationToken);
    }
}

Changelog

  • 1.0.0 - First release
  • 1.0.1 - Bug fix
  • 1.1.0 - Add indexing for timestamp properties
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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.
  • net6.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on EFTimestamps.Annotations:

Package Downloads
EFTimestamps.Configuration

Extensions for the DbContext class to configure the annotations provided in EFTimestamps.Annotations

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.1.0 537 9/25/2022
1.0.1 644 4/20/2020
1.0.0 548 4/14/2020

Bug fix