Microsoft.EntityFrameworkCore.AutoHistory 5.0.8

There is a newer version of this package available.
See the version list below for details.
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 and Polyglot Notebooks. 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 Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (49)

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
6.0.0 167,273 12/14/2022
5.0.8 151,600 10/29/2021
5.0.7 78,171 7/7/2021
5.0.0 116,474 3/12/2021
3.1.3 259,005 4/27/2020
3.1.1 575,310 12/13/2019
3.1.0 6,076 12/11/2019
3.0.1 2,200 12/3/2019
2.1.0 659,906 6/14/2018
2.0.3 127,620 12/26/2017
2.0.2 6,554 10/28/2017
2.0.1 3,527 10/16/2017
2.0.0 8,186 8/15/2017
1.1.2 4,210 5/15/2017
1.1.1 4,107 3/12/2017
1.1.0 2,948 12/18/2016
1.0.0 3,937 11/18/2016