Skip to content
A plugin for Microsoft.EntityFrameworkCore to support automatically recording data changes history.
Branch: master
Clone or download
xyting Merge pull request #32 from paksys/master
Fixed a bug and updated libraries to latest stable version.
Latest commit cc13cb2 Jan 1, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
samples/AspNetCore2.2.MVC.EF.Blogs Refactored prop.OriginalValue check logic Dec 29, 2018
src/Microsoft.EntityFrameworkCore.AutoHistory Refactored prop.OriginalValue check logic Dec 29, 2018
test/Microsoft.EntityFrameworkCore.AutoHistory.Test
.editorconfig
.gitattributes feat: Initial commit Oct 17, 2016
.gitignore
AutoHistory.sln Refactored prop.OriginalValue check logic Dec 29, 2018
LICENSE Initial commit Oct 17, 2016
NuGet.config
README.md Update README.md Dec 26, 2018

README.md

AutoHistory

A plugin for Microsoft.EntityFrameworkCore to support automatically recording data changes history.

How to use

AutoHistory will recording all the data changing history in one Table named AutoHistories, this table will recording data UPDATE, DELETE history.

  1. Install AutoHistory Package

Run the following command in the Package Manager Console to install Microsoft.EntityFrameworkCore.AutoHistory

PM> Install-Package Microsoft.EntityFrameworkCore.AutoHistory

  1. Enable AutoHistory
public class BloggingContext : DbContext
{
    public BloggingContext(DbContextOptions<BloggingContext> options)
        : base(options)
    { }

    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // enable auto history functionality.
        modelBuilder.EnableAutoHistory();
    }
}
  1. Ensure AutoHistory in DbContext. This must be called before bloggingContext.SaveChanges() or bloggingContext.SaveChangesAsync().
bloggingContext.EnsureAutoHistory()

Use Custom AutoHistory Entity

You can use a custom auto history entity by extending the Microsoft.EntityFrameworkCore.AutoHistory class.

class CustomAutoHistory : AutoHistory
{
    public String CustomField { get; set; }
}

Then register it in the db context like follows:

modelBuilder.EnableAutoHistory<CustomAutoHistory>(o => { });

Then provide a custom history entity creating factory when calling EnsureAutoHistory. The example shows using the factory directly, but you should use a service here that fills out your history extended properties(The properties inherited from AutoHistory will be set by the framework automatically).

db.EnsureAutoHistory(() => new CustomAutoHistory()
                    {
                        CustomField = "CustomValue"
                    });

Integrate AutoHistory into other Package

Microsoft.EntityFrameworkCore.UnitOfWork had integrated this package.

You can’t perform that action at this time.