Updating Sql Database to use Temporal Tables with Entity Framework Migration

In a couple of recent applications that IntelliTect developed for clients, we decided to use SQL Server Temporal Tables in order to track changes in the database over time. The apps we were working on were ASP.NET Core web applications, using Entity Framework Core (EF).  While we could have simply modified the databases using queries against the database server directly, we chose to perform an EF migration to add the system versioning.  This way, we could bring up a new database at any time using the standard EF migration pattern, without any extra external steps.

Adding temporal tables to a migration was very simple:

  1. Create a new empty migration
    1. dotnet ef migrations add AddTemporalTables
    1. This will create a skeleton migration with an Up(MigrationBuilder migrationBuilder) and a Down(MigrationBuilder migrationBuilder) method, both of which contain no code
  2. Modify the skeleton migration as shown below
  3. Apply the migration
    1. dotnet ef database update

Migration Code

Leave a comment

Your email address will not be published. Required fields are marked *