Reputation: 4983
I need to rename the primary key of an existing table through FluentMigrator
so an automapper can automatically detect the column.
For most columns, it's a simple 1)
delete any foreign key constraints on that column 2)
delete indices for that column and 3)
rename the column. I have historically done this by:
Delete.ForeignKey("foreignkeyconstraint").OnTable("mytable");
Delete.Index("UserId").OnTable("mytable");
Rename.Column("UserId").OnTable("mytable").To("UserInfo_id");
However, this doesn't appear to work for primary keys, since I can't delete the automatically created index on that column. What is the correct way to rename a primary key column with FluentMigrator
?
Upvotes: 10
Views: 7856
Reputation: 3584
Use the following method call to rename your primary key (SQL Server)
Execute.Sql("EXEC sp_rename N'[Current_Primary_Key_Name]', '[New_Primary_Key_Name]', 'object';");
Upvotes: 6
Reputation: 7969
Something like this should work as long as it is not an identity (auto increment) column as well:
Delete.PrimaryKey("PRIMARY KEY").FromTable("mytable");
Upvotes: 3