Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use System-Versioned Temporal Table with Entity Framework?

Tags:

I can use temporal tables in SQL Server 2016. Entity Framework 6 unfortunately does not know this feature yet. Is there the possibility of a workaround to use the new querying options (see msdn) with Entity Framework 6?

I created a simple demo project with an employee temporal table:

enter image description here

I used the edmx to map the table to entity (thanks to Matt Ruwe):

enter image description here

Everything works fine with pure sql statements:

using (var context = new TemporalEntities()) {     var employee = context.Employees.Single(e => e.EmployeeID == 2);     var query =        $@"SELECT * FROM [TemporalTest].[dbo].[{nameof(Employee)}]          FOR SYSTEM_TIME BETWEEN          '0001-01-01 00:00:00.00' AND '{employee.ValidTo:O}'          WHERE EmployeeID = 2";     var historyOfEmployee = context.Employees.SqlQuery(query).ToList(); }     

Is it possible to add the history functionality to every entity without pure SQL? My solution as entity extension with reflection to manipulate the SQL query from IQuerable isn't perfect. Is there an existing extension or library to do this?

edit: (Based on the commentary by Pawel)

I tried to use a Table-valued Function:

CREATE FUNCTION dbo.GetEmployeeHistory(     @EmployeeID int,      @startTime datetime2,      @endTime datetime2) RETURNS TABLE AS RETURN  (     SELECT          EmployeeID,         [Name],          Position,          Department,          [Address],         ValidFrom,         ValidTo     FROM dbo.Employee     FOR SYSTEM_TIME BETWEEN @startTime AND @endTime     WHERE EmployeeID = @EmployeeID ); 
using (var context = new TemporalEntities()) {     var employee = context.Employees.Single(e => e.EmployeeID == 2);     var historyOfEmployee =       context.GetEmployeeHistory(2, DateTime.MinValue, employee.ValidTo).ToList(); }  

Do I have to create a function for each entity or is there a generic option?

like image 284
cSteusloff Avatar asked Jan 06 '17 21:01

cSteusloff


People also ask

What two classes are required for temporal table?

A temporal table must have a primary key defined in order to correlate records between the current table and the history table, and the history table can't have a primary key defined.

What is a system-versioned temporal table?

Temporal tables (also known as system-versioned temporal tables) are a database feature that brings built-in support for providing information about data stored in the table at any point in time, rather than only the data that is correct at the current moment in time.

How do I get rid of system-versioned temporal table?

To drop a temporal table, you have to follow three steps: Alter the current table and set off the system versioning. Drop the current table. Drop the history table.


1 Answers

No, I am afraid, you cannot. I have been back & forth with Microsoft gurus on this front.

This is a known issue. And the best advice I have found is to use FromSql as explained here.

like image 200
Stephen Witherden Avatar answered Oct 24 '22 14:10

Stephen Witherden