Entity Framework Core and SQL Server 2016 temporal tables
Asked Answered
P

5

7

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.

Penult answered 6/3, 2017 at 14:2 Comment(0)
P
5

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; } 
Penult answered 6/3, 2017 at 17:28 Comment(0)
K
3

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

Kail answered 6/3, 2017 at 14:25 Comment(2)
Any solution to bypass the 'GENERATED ALWAYS' columns during INSERT/UPDATE in database first approach. ? I generate models using DBScaffold command and I cannot use the solution that is marked as answer here as everything gets overwritten each time I generate the model from database. #52655109Yokefellow
this should be as a comment, no as a solution, in fact there is a solutionBurgwell
Y
0

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 
Yokefellow answered 8/11, 2018 at 20:40 Comment(0)
P
0

I think there's a better solution for this as follows: Create partial context file (to prevent re-making the changes after re-generating the model) as follows

public partial class DatabaseDBContext : DbContext
{
    partial void OnModelCreatingPartial(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Table1>(entity =>
        {
            entity.Property(e => e.StartTime)
                .ValueGeneratedOnAddOrUpdate();
            entity.Property(e => e.EndTime)
                .ValueGeneratedOnAddOrUpdate();
        });
    }
}
Persistent answered 8/4, 2020 at 15:51 Comment(0)
H
0

.NET 6 and Entity Framework Core 6.0 supports SQL Server temporal tables out of the box.

See this answer for examples:

https://mcmap.net/q/139110/-net-core-entity-framework-and-sql-server-temporal-tables-automatic-scaffolding

Hyponasty answered 18/11, 2021 at 9:59 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.