EFTimestamps 1.0.0

A library for handling soft deletes & timestamps in Entity Framework.

Install-Package EFTimestamps -Version 1.0.0
dotnet add package EFTimestamps --version 1.0.0
<PackageReference Include="EFTimestamps" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EFTimestamps --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

EFTimestamps

A library for handling timestamps &amp; soft deletes in Entity Framework.

Documentation

  • Introduction
  • Getting started
  • Usage
  • Methods
  • Notes

Introduction

EFTimestamps is a library that control the dates your models have been created, updated, or (soft) deleted, automatically handling all CRUD operations in the DB context.
When models are soft deleted, they are not actually deleted from the database, instead a DeletedAt column is set to the date the model has been deleted. If a model's DeletedAt value is not null, and is set to a specific date, it means it has been deleted.

Getting started

EFTimestamps library is available in nuget.org. To install it, run the following command in the Package Manager Console:
Install-Package EFTimestamps

Next, let your DB context inheirt from TimestampsDbContext, as follows:

public class ApplicationDbContext : TimestampsDbContext
{
}

Usage

For a model to have the columns CreatedAt and UpdatedAt, let it implement the ITimestamps interface and all its members, as follows:

public class Person : ITimestamps
{
  public int Id { get; set; }
  public string Name { get; set; }
  
  // ITimestamps implementation
  public DateTime CreatedAt { get; set; }
  public DateTime UpdatedAt { get; set; }
}

Along with timestamps, a model can be soft-deletable by implementing the ISoftDeletable interface as follows:

public class Person : ITimestamps, ISoftDeletable
{
  public int Id { get; set; }
  public string Name { get; set; }
  
  // ITimestamps implementation
  public DateTime CreatedAt { get; set; }
  public DateTime UpdatedAt { get; set; }
  
  // ISoftDeletable implementation
  public DateTime? DeletedAt { get; set; }
}

That's it! Your model can now have timestamps via the CreatedAt and UpdatedAt columns, as well as be soft deleted via the DeletedAt columns.

Methods

You can use the following helper methods to extend your use of soft deletes:

  • Soft deleting a model:
var person = DbContext.Persons.First();
DbContext.Persons.Remove(person);
  • Restoring a model:
DbContext.Persons.Restore(person);
  • Checking if a model is deleted:
person.IsDeleted();
  • If you don't want a model to be soft-deleted, and want it permanently removed, you can force-remove it:
DbContext.Persons.ForceRemove(person);

Notes

TimestampsDbContext is currently incompatible with IdentityDbContext used in ASP.NET MVC projects. It will however be available hopefully in the next versions of the library.

EFTimestamps

A library for handling timestamps &amp; soft deletes in Entity Framework.

Documentation

  • Introduction
  • Getting started
  • Usage
  • Methods
  • Notes

Introduction

EFTimestamps is a library that control the dates your models have been created, updated, or (soft) deleted, automatically handling all CRUD operations in the DB context.
When models are soft deleted, they are not actually deleted from the database, instead a DeletedAt column is set to the date the model has been deleted. If a model's DeletedAt value is not null, and is set to a specific date, it means it has been deleted.

Getting started

EFTimestamps library is available in nuget.org. To install it, run the following command in the Package Manager Console:
Install-Package EFTimestamps

Next, let your DB context inheirt from TimestampsDbContext, as follows:

public class ApplicationDbContext : TimestampsDbContext
{
}

Usage

For a model to have the columns CreatedAt and UpdatedAt, let it implement the ITimestamps interface and all its members, as follows:

public class Person : ITimestamps
{
  public int Id { get; set; }
  public string Name { get; set; }
  
  // ITimestamps implementation
  public DateTime CreatedAt { get; set; }
  public DateTime UpdatedAt { get; set; }
}

Along with timestamps, a model can be soft-deletable by implementing the ISoftDeletable interface as follows:

public class Person : ITimestamps, ISoftDeletable
{
  public int Id { get; set; }
  public string Name { get; set; }
  
  // ITimestamps implementation
  public DateTime CreatedAt { get; set; }
  public DateTime UpdatedAt { get; set; }
  
  // ISoftDeletable implementation
  public DateTime? DeletedAt { get; set; }
}

That's it! Your model can now have timestamps via the CreatedAt and UpdatedAt columns, as well as be soft deleted via the DeletedAt columns.

Methods

You can use the following helper methods to extend your use of soft deletes:

  • Soft deleting a model:
var person = DbContext.Persons.First();
DbContext.Persons.Remove(person);
  • Restoring a model:
DbContext.Persons.Restore(person);
  • Checking if a model is deleted:
person.IsDeleted();
  • If you don't want a model to be soft-deleted, and want it permanently removed, you can force-remove it:
DbContext.Persons.ForceRemove(person);

Notes

TimestampsDbContext is currently incompatible with IdentityDbContext used in ASP.NET MVC projects. It will however be available hopefully in the next versions of the library.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0 568 11/29/2017