To change the IDENTITY property of a column, the column needs to be dropped and recreated
See original GitHub issueChanging an identity pk to composite key
dotnet : System.InvalidOperationException: To change the IDENTITY property of a column, the column needs to be dropped and recreated.
At line:1 char:1
+ dotnet ef migrations script
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (System.InvalidO... and recreated.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
at Microsoft.EntityFrameworkCore.Migrations.SqlServerMigrationsSqlGenerator.Generate(AlterColumnOperation operation, IModel mod
el, MigrationCommandListBuilder builder)
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(MigrationOperation operation, IModel model,
MigrationCommandListBuilder builder)
at Microsoft.EntityFrameworkCore.Migrations.MigrationsSqlGenerator.Generate(IReadOnlyList`1 operations, IModel model)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateUpSql(Migration migration)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.GenerateScript(String fromMigration, String toMigration, Boolean idempotent)
at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.ScriptMigration(String fromMigration, String toMigration, Boolean
idempotent, String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.ScriptMigration.<>c__DisplayClass0_1.<.ctor>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
[31m[1mTo change the IDENTITY property of a column, the column needs to be dropped and recreated.[22m[39m
Steps to reproduce
public class A {
[Key]
public int Key {get; set;}
public int Key2 {get; set; }
}
// 1. Create DB
public class A {
public int Key {get; set; }
public int Key2 {get; set; }
}
modelBuilder.Entity<A>().HasKey(e => new { e.Key, e.Key2 });
// 2. Try migrate (error message will be shown). **dotnet ef migrations script** fails as well
// **Incorrect** Migration.cs
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropPrimaryKey(
name: "PK_A",
table: "A");
migrationBuilder.AlterColumn<int>(
name: "Key",
table: "A",
nullable: false,
oldClrType: typeof(int))
.OldAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
}
// ** Correct** Migration.cs
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "InfluencerId",
table: "OfferingInfluencer",);
migrationBuilder.AddColumn<int>(
name: "InfluencerId",
table: "OfferingInfluencer",
nullable: false);
}
Can’t recall if doing schema compare in VS created the temporary table and did the data moving or not. For now I just deleted the previous data so no data migration needed to take place, but perhaps EF generates the same as Schema Compare does (or don’t 😃
Further technical details
EF Core version: 1.1.0 Database Provider: Microsoft.EntityFrameworkCore.SqlServer Operating system: Windows 10 IDE: Visual Studio Community 2017 RC
Issue Analytics
- State:
- Created 7 years ago
- Comments:14 (3 by maintainers)
Top Results From Across the Web
c# - Change the IDENTITY property of a column, ...
First, just to recap: to change the identity property, ... the IDENTITY property of a column, the column needs to be dropped and...
Read more >To change the IDENTITY property of a column, ...
Open SSMS and manually change the column that's erroring to not be an identity column (right click on table > design > ID...
Read more >C# : Change the IDENTITY property of a column ... - YouTube
C# : Change the IDENTITY property of a column, the column needs to be dropped and recreated To Access My Live Chat Page,...
Read more >EF Core 6 Identity value generation problem
Hello, I am using EF Core 6 and SQL server express and trying to migrate my entities. I am getting this error. The...
Read more >Add or drop identity property for an existing SQL Server ...
The only clean way to do this is to create a new column and make it an identity column or create a new...
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
I get this problem whilst following this tutorial, with the latest version of EF Core.
https://www.codeproject.com/Articles/1210559/Asp-net-core-Angular-Build-from-scratch-a-web-appl
All the tutorial is attempting to do is add one table to a blank existing SQL Server database (on localhost), and I get this error about “To change the IDENTITY property of a column, the column needs to be dropped and recreated.”
Which damn table is it referring to ?!
Why don’t the error messages quote the SQL it’s fallen over on ?
Yesterday, I couldn’t even get most of that tutorial to build, until i upgraded VS2017 to the latest version. VS Core didn’t even recognise that I’d added AutoMapper to my project. Today, following the upgrade, the code does build okay (but this IDENTITY error continues).
Last week, I was trying to follow an ASP.Net Core tutorial on Microsoft’s own website. It was 5 months old, and already out of date, and hopeless. I was Googling for unexpected error messages within 13 minutes of starting the tutorial.
This whole ASP.Net Core and EF Core stuff… it’s just not stable.
Even now, in November 2017, it’s just not ready yet. Far from it.
I’ve never wasted so many man-hours Googling for error message after error message.
Still the same issue…