Microsoft.EntityFrameworkCore.AutoHistory 5.0.8

.NET 5.0
dotnet add package Microsoft.EntityFrameworkCore.AutoHistory --version 5.0.8
NuGet\Install-Package Microsoft.EntityFrameworkCore.AutoHistory -Version 5.0.8
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<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
#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

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.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (47)

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

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

HadesGeneric.Data

Generic Repository, UnitOfwork.

GitHub repositories (3)

Showing the top 3 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.
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.
HenJigg/my-todoapp
该项目为2022年WPF项目实战合集源代码
Version Downloads Last updated
5.0.8 58,675 10/29/2021
5.0.7 59,092 7/7/2021
5.0.0 76,775 3/12/2021
3.1.3 204,984 4/27/2020
3.1.1 376,013 12/13/2019
3.1.0 4,074 12/11/2019
3.0.1 1,617 12/3/2019
2.1.0 585,185 6/14/2018
2.0.3 118,620 12/26/2017
2.0.2 4,995 10/28/2017
2.0.1 2,803 10/16/2017
2.0.0 5,914 8/15/2017
1.1.2 2,830 5/15/2017
1.1.1 2,038 3/12/2017
1.1.0 1,538 12/18/2016
1.0.0 2,440 11/18/2016