EFTimestamps 1.0.0
Install-Package EFTimestamps -Version 1.0.0
dotnet add package EFTimestamps --version 1.0.0
<PackageReference Include="EFTimestamps" Version="1.0.0" />
paket add EFTimestamps --version 1.0.0
#r "nuget: EFTimestamps, 1.0.0"
// Install EFTimestamps as a Cake Addin
#addin nuget:?package=EFTimestamps&version=1.0.0
// Install EFTimestamps as a Cake Tool
#tool nuget:?package=EFTimestamps&version=1.0.0
EFTimestamps
A library for handling timestamps & 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.
Product | Versions |
---|---|
.NET Framework | net45 net451 net452 net46 net461 net462 net463 net47 net471 net472 net48 |
-
- EntityFramework (>= 6.0.0)
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 |
---|---|---|
1.0.0 | 942 | 11/29/2017 |