question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EF Core 6.0 temporal table migration when altering computed column not generating correct script

See original GitHub issue

File a bug

Step 1 Make table temporal Step 2 Add migration for that Step 3 Add new computed column to the entity step 4 Add Migration for altered column setp 5 Generate script for migration

Include your code

added new computed column to existing Temporal Table

entity.Property(x => x.Level).HasComputedColumnSql("Id.GetLevel() PERSISTED");

Generate migration

dotnet ef migrations add addinglevel

Generate migration script

dotnet ef migrations script 20210624210145_AddTemporalSupport 20220419235406_addinglevel
Build started...
Build succeeded.
BEGIN TRANSACTION;
GO

ALTER TABLE [HierarchyNode] ADD [Level] AS Id.GetLevel() PERSISTED;
GO

INSERT INTO [__EFMigrationsHistory] ([MigrationId], [ProductVersion])
VALUES (N'20220419235406_addinglevel', N'6.0.4');
GO

COMMIT;
GO

When running migration in SQL Studio

Msg 13724, Level 16, State 1, Line 1
System-versioned table schema modification failed because adding computed column while system-versioning is ON is not supported.

Expected

Migration script to turn of versioning and alter both table and turn versioning on

EF Core version: 6.0.4 Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer) Target framework: (e.g. .NET 6.0) Operating system: Windows IDE: (e.g. Visual Studio 2022 17.2)

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
omarfqcommented, Sep 23, 2022

Had a similar issue while adding a new migration after altering a column name or adding a new column. Steps are similar to @siby-george’s:

  1. Make temporal table.
  2. Add new migration for that temporal table.
  3. Alter any column name or add new one to the existing entity.
  4. Add migration for altered/added column.
  5. Generate migration script.

Code

Original entity looked like this:

public class ExonerationDetermination : AuditEntity
  {
      public long Id { get; set; }
      public string Determination { get; set; }
      public string Reason { get; set; }
      public string Comments { get; set; }
      public virtual User CreatedByUser { get; set; }
  }

After modification:

public class ExonerationDetermination : AuditEntity
  {
      public long Id { get; set; }
      public string Determination { get; set; }
      public string Reason { get; set; }
      public string AdditionalComments { get; set; } // Modified
      public virtual User CreatedByUser { get; set; }
      public string PreviousStatus { get; set; } // Added
      public string NewStatus { get; set; } // Added
  }

Generate migration with command:

dotnet ef migrations add ExonerationRequests

When building and starting the project, the following error is displayed on the output window:

Setting SYSTEM_VERSIONING to ON failed because table 'PEJC.exoneration.Determination' has 14 columns and table 'PEJC.exoneration.DeterminationHistory' has 11 columns.

The three columns that are missing from the table ‘PEJC.exoneration.DeterminationHistory’ are precisely the ones we added/modified. We verified this in the Down method in the migration file generated by EF Core.

Expected

Migration script should be able to turn off versioning, alter/add columns as specified on the entity, then turn versioning back on.

Additional Info

EF Core version: 6.0.9 Database provider: SQL Server Target framework: .NET 6 Operating system: Windows IDE: Visual Studio 2022 V17.3.4

0reactions
James-Jackson-Southcommented, Nov 3, 2022

@James-Jackson-South The correct script in this case would need to make the table not system-versioned, add the computed column, then make it system-versioned again. However, after discussion we’re not sure if this will work either. It may not be possible to make the table system-versioned if it has a computed column, and even if it is, it’s not clear how that computed column would work when looking at historical data. What is the computed column definition that you want to use?

Sorry @ajcvickers Just saw this. I wasn’t using a computed column in my case but changing the column type. The workaround you suggested is the approach I eventually figured out, but it took me a rather long while to do so.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Framework not working with temporal table
After changing some of the tables in my schema to be temporal tables, I started getting the following error when attempting to insert...
Read more >
SQL Server temporal tables in EF Core 6.0
EF Core 6.0 supports: The creation of temporal tables using EF Core migrations; Transformation of existing tables into temporal tables, again ...
Read more >
EF Core and MS SQL Server: Adding Migrations for a ...
So let's go by an example. I am adding a new computed column to my target table PropertyAccount and it's already System-Versioned. public...
Read more >
Entity Framework Core Migrations
The migrations feature in Entity Framework Core enables you to make changes to your model and then propagate those changes to your database ......
Read more >
Data Seeding - EF Core
Data seeding is the process of populating a database with an initial set of data. There are several ways this can be accomplished...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found