EF Core 6.0 temporal table migration when altering computed column not generating correct script
See original GitHub issueFile 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:
- Created a year ago
- Reactions:1
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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:
Code
Original entity looked like this:
After modification:
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
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.