https://github.com/mrnkr/eftimestamps
Put timestamps in your entities the easy way
https://github.com/mrnkr/eftimestamps
dotnet-core entity-framework-core timestamps
Last synced: 7 months ago
JSON representation
Put timestamps in your entities the easy way
- Host: GitHub
- URL: https://github.com/mrnkr/eftimestamps
- Owner: mrnkr
- License: mit
- Created: 2020-04-14T00:49:11.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-04-25T20:26:18.000Z (over 2 years ago)
- Last Synced: 2025-01-24T04:43:36.115Z (9 months ago)
- Topics: dotnet-core, entity-framework-core, timestamps
- Language: C#
- Homepage: https://www.nuget.org/packages/EFTimestamps.Configuration/
- Size: 42 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# EFTimestamps
[![NuGet version][nuget-image]][nuget-url]
[![Downloads][downloads-image]][nuget-url]
[](https://travis-ci.com/mrnkr/EFTimestamps)
[](https://codecov.io/gh/mrnkr/EFTimestamps)
[![License][license]][nuget-url][nuget-image]:https://img.shields.io/nuget/v/EFTimestamps.Annotations
[nuget-url]:https://www.nuget.org/packages/EFTimestamps.Annotations
[downloads-image]:https://img.shields.io/nuget/dt/EFTimestamps.Annotations
[license]:https://img.shields.io/github/license/mrnkr/EFTimestampsPut 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`](https://www.nuget.org/packages/EFTimestamps.Annotations) and [`EFTimestamps.Configuration`](https://www.nuget.org/packages/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.
```cs
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.
```cs
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 SaveChangesAsync(bool acceptAllChangesOnSuccess, CancellationToken cancellationToken = default)
{
this.UpdateTimestamps();
return base.SaveChangesAsync(acceptAllChangesOnSuccess, cancellationToken);
}public override Task 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