I am using EF Core 2.1
This was my initial model definition.
public class Customer //Parent
{
public int Id { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public BankAccount BankAccount { get; set; }
}
public class BankAccount
{
public int Id { get; set; }
public string Branch { get; set; }
public string AcntNumber { get; set; }
public DateTime CreatedDate { get; set; }
public int CustomerId { get; set; }
public Customer Customer { get; set; }
}
But I realized having Id & CustomerId both is overhead as its One-to-One relation, I can update my BankAccount model definition as below.
public class BankAccount
{
public int Id { get; set; }
public string Branch { get; set; }
public string AcntNumber { get; set; }
public DateTime CreatedDate { get; set; }
public Customer Customer { get; set; }
}
While in DbContext class defined the principal entity as below.
HasOne(b => b.Customer).WithOne(c => c.BankAccount).HasForeignKey<BankAccount>(f => f.Id);
While running the update-database I am getting the below error.
System.InvalidOperationException: To change the IDENTITY property of a column, the column needs to be dropped and recreated.
However, ideally I should not but just get rid of this error, I deleted the column, constraints and as well table and then the complete database as well. But still the same error.
This error occurs when you try to alter or modify a table that already exists when you want to change schema or the table that already exists, which EF core Doesn't support it yet it needs manual action. here is what you can do about this:
I had this problem when I tried to change a model from public byte Id {get; set;} to public int Id {get; set;}.
To face the issue, I did the following things:
Remove-Migration -Project <target_project> in the Package Manager ConsoleModelSnapshot file and paste them in your branch (overwrite them carefully!).add-migration <migration_name> in the Package Manager Consoleupdate-database in the Package Manager ConsoleI can solve it in this way because my code was not in a production environment. Maybe you have to face another complex issues if the model is already in there.
I ran into the same problem, and I solved it by two steps and two migrations:
Step 1
Step 2
In my case, table SharedBalances is renamed to Balances and it's identity column SharedBalancesId is renamed BalanceId. SQL commands are executed on SQL Server. You can also try migrationBuilder.Sql(my_sql_command_here)
I created the migration and got the same error.
Rename the column and the table using TSQL command:
EXEC sp_RENAME 'SharedBalances.SharedBalanceId', 'BalanceId', 'COLUMN';
EXEC sp_RENAME 'SharedBalances', 'Balances';
-- Caution: Changing any part of an object name could break scripts and stored procedures.
Comment the RenameTable command in your migration:
/*
migrationBuilder.RenameTable(
name: "SharedBalances",
newName: "Balances");
*/
Comment the AddPrimaryKey command in your migration:
/*
migrationBuilder.DropPrimaryKey(
name: "PK_SharedBalances",
table: "Balances");
migrationBuilder.AddPrimaryKey(
name: "PK_Balances",
table: "Balances",
column: "BalanceId");
*/
Update occurences of the table name DropForeignKey commands in your migration:
From this....
migrationBuilder.DropForeignKey(
name: "FK_SharedBalances_Users_OwnerUserId",
table: "SharedBalances");
migrationBuilder.DropPrimaryKey(
name: "PK_SharedBalances",
table: "SharedBalances");
To this:
migrationBuilder.DropForeignKey(
name: "FK_SharedBalances_Users_OwnerUserId",
table: "Balances");
migrationBuilder.DropPrimaryKey(
name: "PK_SharedBalances",
table: "Balances");
Now your migration will work. This is how it happened:
please follow this step:
1-please do all change of identity column in sql server(not in your code first entity framework)
2-comment identity column changes in the migration (.cs file)
3-update-database
enjoy that
I had to:
Done
In my opinion running the EF Migrations against anything but your development database is asking for trouble as you are naturally limited by the fact that EF migrations will sometimes flatly refuse to work when altering the structure of you objects (changing primary keys and changing foreign keys being the most often encountered).
For many years I have used tools to ensure DB schema is included in version control (complementary to EF migrations). Do your developments to change your dev database (where the data is not important), create multiple migrations but then use the tools to roll these up into a DB deployment script.
Here’s a summary of what I would do in this case: -
BankAccountBankAccount class with it’s corrected definitionThe reality is if you have production data that you want to radically change the structure of with a code first approach it will probably end badly for you unless you understand and address the data migration from one structure to the other.
I ran into the same problem ( In my case I didn't have any data in the tables ), and I solved it in this way ( It's not the proper way, but it worked for me ):
For those who are lazy like me: You want to change the datatype of a primary key column "Id" from int to Guid in a table called "Translations" as my case was.
migrationBuilder.AlterColumn<Guid>( name: "Id", table: "Translations", type: "uniqueidentifier", nullable: false, oldClrType: typeof(int), oldType: "int") OldAnnotation("SqlServer:Identity", "1, 1");
You can delete or comment that out
migrationBuilder.DropPrimaryKey( name: "PK_Translations", table: "Translations");
migrationBuilder.DropColumn( name: "Id", table: "Translations");
migrationBuilder.AddColumn<Guid>( name: "Id", table: "Translations", type: "uniqueidentifier", nullable: false);
Remember to do the opposite in the Down override method in case you may want to reverse the migration
I had a similar problem where I was changing the relational navigation component of a table's configuration from WithMany to WithRequiredDependent. Entity framework wanted to drop the index and recreate the column, even though nothing in the database should have changed.
To fix this, I rescaffolded the latest migration which allowed entity to absorb the change without any new migration being created. You can rescaffold the latest migration by reverting the migration from the target database, and re-running the Add-Migration script for the latest migration with the exact same migration name.
entity.ToTable("BankAccount2")Add-Migration BankAccountTempChangesUpdate-Databaseentity.ToTable("BankAccount")Add-Migration BankAccountOkUpdate-Database