findulov / EntityFrameworkCore.TemporalTables

Extension library which allows developers to easily use temporal tables in SQL Server using Entity Framework Core.
MIT License
63 stars 19 forks source link

EntityFrameworkCore.TemporalTables

Extension library for Entity Framework Core which allows developers who use SQL Server to easily use temporal tables.

How to use it?

Service Provider configuration

  1. Use UseInternalServiceProvider() on DbContextOptionsBuilder when registering your DbContext to replace Entity Framework's internal service provider with yours. For example:
services.AddDbContextPool<MyDbContext>((provider, options) =>
{
    options.UseSqlServer(configuration.GetConnectionString("DefaultConnection"));
    options.UseInternalServiceProvider(provider);
});
  1. Then register all Entity Framework services into your service provider by using: services.AddEntityFrameworkSqlServer();

  2. And finally call: services.RegisterTemporalTablesForDatabase<MyDbContext>();

Use the first two settings only when you want your temporal tables to be executed by default when using Update-Database command from Package Manager Console or DbContext.Database.Migrate() / DbContext.Database.MigrateAsync().

If you want to handle the temporal tables SQL execution yourself, you can do it manually by using:

var temporalTableSqlExecutor = serviceProvider.GetService<ITemporalTableSqlExecutor<MyDbContext>>();
temporalTableSqlExecutor.Execute();


DbContext configuration

In OnModelCreating(ModelBuilder modelBuilder) method you have the following options:


You can refer to the sample application for more configuration information.

You can install the NuGet package from here: https://www.nuget.org/packages/EntityFrameworkCore.TemporalTables/