Programmers learn & share
0 votes

Problem :

My DbContext class defined the principal entity as below.

HasOne(b => b.Customer).WithOne(c => c.BankAccount).HasForeignKey<BankAccount>(f => f.Id);

But while running the update-database I am getting the following error:

 System.InvalidOperationException: To change the IDENTITY property of a column, the column needs to be dropped and recreated.

But ideally I should not get it but just get rid of this error. I have deleted the column, constraints and table as well and then the complete database also. But still I am getting the same error.

by (7.5k points)   | 596 views

1 Answer

0 votes

Solution :

I also had faced this issue when I tried to change my model from public byte Id {get; set;} to public int Id {get; set;}. To solve the issue, I did the following things:

1.  Removed all the migrations until the creation of the target model with Remove-Migration -Project <target_project> command in the Package Manager Console

2.  Deleted the actual database

3.  If you are having some migrations in the middle that you have not created for instance they are from another branch then copy the migrations files and also the ModelSnapshot file and paste them in your branch very carefully!

4.  created a new migration with add-migration <migration_name> command in the Package Manager Console

5.  updated the database with update-database command in the Package Manager Console

I was able solve the issue in this way as my code was not in a production environment.

by (36.1k points)  
2,227 questions
2,734 answers
241 users