Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework Core and SQL Server 2016 temporal tables

We are using EF Core and SQL Server 2016 for our .NET Core Web API. I am evaluating use of temporal tables and its impact on EF Core code. When I generate the EF model using cmdline then it generates model with appstart, append and mappings in dbcontext. When I do insert/update they fail saying these columns cannot be updated. I had to remove appstart, end from model and dbcontext mapping to make it work. I read there is no interception capability yet in EF Core like EF 6.x.

Please advise about any better solutions for this.

like image 638
krishnakumar Avatar asked Mar 06 '17 14:03

krishnakumar


3 Answers

I tried below options and they are working.

  1. option 1: removed appstart, append fields from entities and dbcontext mappings and updates/insert started working properly.

  2. option 2: decorate period columns with attributes as below.


[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime StartTime { get; set; }

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public DateTime EndTime { get; set; } 
like image 63
krishnakumar Avatar answered Oct 20 '22 10:10

krishnakumar


There currently is no better solution for this, and the feature is on the backlog.

like image 3
ErikEJ Avatar answered Oct 20 '22 10:10

ErikEJ


Making the Period start column(StartTime) and Period end column(EndTime) hidden should fix this issue. We can do this by

ALTER TABLE [dbo].[Table1] ALTER COLUMN [StartTime] ADD HIDDEN;
ALTER TABLE [dbo].[Table1] ALTER COLUMN [EndTime] ADD HIDDEN;

We can see the settings for hidden against these columns in the sys.columns table

SELECT * FROM sys.columns WHERE is_hidden = 1 
like image 1
VPP Avatar answered Oct 20 '22 11:10

VPP