Skip to content

mrnkr/EFTimestamps

Repository files navigation

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