Microsoft.EntityFrameworkCore.AutoHistory 5.0.8

Install-Package Microsoft.EntityFrameworkCore.AutoHistory -Version 5.0.8
dotnet add package Microsoft.EntityFrameworkCore.AutoHistory --version 5.0.8
<PackageReference Include="Microsoft.EntityFrameworkCore.AutoHistory" Version="5.0.8" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Microsoft.EntityFrameworkCore.AutoHistory --version 5.0.8
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Microsoft.EntityFrameworkCore.AutoHistory, 5.0.8"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install Microsoft.EntityFrameworkCore.AutoHistory as a Cake Addin
#addin nuget:?package=Microsoft.EntityFrameworkCore.AutoHistory&version=5.0.8

// Install Microsoft.EntityFrameworkCore.AutoHistory as a Cake Tool
#tool nuget:?package=Microsoft.EntityFrameworkCore.AutoHistory&version=5.0.8
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

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()

If you want to record data changes for all entities (except for Added - entities), just override SaveChanges and SaveChangesAsync methods and call EnsureAutoHistory() inside overridden version:

public class BloggingContext : DbContext
{
    public BloggingContext(DbContextOptions<BloggingContext> options)
        : base(options)
    { }
    
    public override int SaveChanges()
    {
        this.EnsureAutoHistory();
        return base.SaveChanges();
    }
    
    public override Task<int> SaveChangesAsync(CancellationToken cancellationToken = new CancellationToken())
    {
        this.EnsureAutoHistory();
        return base.SaveChangesAsync(cancellationToken);
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        // enable auto history functionality.
        modelBuilder.EnableAutoHistory();
    }
}
  1. If you also want to record Added - Entities, which is not possible per default, override SaveChanges and SaveChangesAsync methods this way:
public class BloggingContext : DbContext
{
    public override int SaveChanges()
    {
        var addedEntities = this.ChangeTracker
                                .Entries()
                                .Where(e => e.State == EntityState.Added)
                                .ToArray(); // remember added entries,
        // before EF Core is assigning valid Ids (it does on save changes, 
        // when ids equal zero) and setting their state to 
        // Unchanged (it does on every save changes)
        this.EnsureAutoHistory();
        base.SaveChanges();

        // after "SaveChanges" added enties now have gotten valid ids (if it was necessary)
        // and the history for them can be ensured and be saved with another "SaveChanges"
        this.EnsureAddedHistory(addedEntities);
        base.SaveChanges();
    }   
}

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"
                    });

Excluding properties from AutoHistory

You can now excluded properties from being saved into the AutoHistory tables by adding a custom attribute[ExcludeFromHistoryAttribute] attribute to your model properties.

    public class Blog
    {        
        [ExcludeFromHistory]
        public string PrivateURL { get; set; }
    }

Integrate AutoHistory into other Package

Microsoft.EntityFrameworkCore.UnitOfWork had integrated this package.

NuGet packages (43)

Showing the top 5 NuGet packages that depend on Microsoft.EntityFrameworkCore.AutoHistory:

Package Downloads
Microsoft.EntityFrameworkCore.UnitOfWork

A plugin for Microsoft.EntityFrameworkCore to support repository, unit of work patterns, and multiple database with distributed transaction supported.

AspCore.DataAccess

Package Description

Threenine.Data

A simple generic repository wrapper library for entity framework core

iCom

iCom integrates a variety of specific operation classes, such as: Cookie, Items, Cache, Form, DbContext, DbDynamic, DbConn, SqlQuery, FileUtil, FileUpload, Crypto, BarCode, StringUtil, TypeConvert, SortList, DateUtil, RegexUtil, Environal, ReflectUtil, HttpClient, ImageUtil, XmlUtil, ZipPackage, WordUtil, ExcelUtil, PdfUtil, PinyinConvert, PdfToImageConverter, JsonConfig, XmlConfig, ObjectCopy, TagHelper, SkipUrlMiddleware and so on. // HttpContext Called in the ConfigureServices method services.AddContextAccessor(); // Called in the Configure method app.UseHttpContext(); // EfCore Called in the ConfigureServices method // Application Assembly var assembly = Assembly.GetExecutingAssembly(); // Register Database Models services.AddDbContext(assembly); // Auto Update Database services.AddMigrate(); // Register All Services services.AddService(assembly);

NViser.Core.Data

NViser.Core.Data is including dto and entity classes for services and databases

GitHub repositories (4)

Showing the top 4 popular GitHub repositories that depend on Microsoft.EntityFrameworkCore.AutoHistory:

Repository Stars
Arch/UnitOfWork
A plugin for Microsoft.EntityFrameworkCore to support repository, unit of work patterns, multiple database with distributed transaction supported, and MySQL multiple databases/tables sharding supported.
HenJigg/WPF-Examples
WPF sample program based on Prism
SaiGonSoftware/Awesome-CMS-Core
Awesome CMS Core is an open source CMS built using ASP.Net Core & ReactJS with module seperation concern in mind and provide lastest trend of technology like .Net Core, React, Webpack, SASS, Background Job, Message Queue.
threenine/Threenine.Data
Generic Repository Pattern for .net core
Version Downloads Last updated
5.0.8 1,501 10/29/2021
5.0.7 17,893 7/7/2021
5.0.0 37,527 3/12/2021
3.1.3 118,417 4/27/2020
3.1.1 183,514 12/13/2019
3.1.0 2,849 12/11/2019
3.0.1 1,413 12/3/2019
2.1.0 473,815 6/14/2018
2.0.3 115,320 12/26/2017
2.0.2 4,586 10/28/2017
2.0.1 2,403 10/16/2017
2.0.0 5,316 8/15/2017
1.1.2 2,548 5/15/2017
1.1.1 1,676 3/12/2017
1.1.0 1,243 12/18/2016
1.0.0 1,826 11/18/2016