1

I have the following code in my EF migrations Up() method

AlterColumn("dbo.ActivityType", "Id", c => c.Int(nullable: false, identity: true));

This generates the following SQL:

ALTER TABLE [dbo].[ActivityType] ALTER COLUMN [Id] [int] NOT NULL

Which isn`t really setting my column as an identity, ideally it should drop the column and recreate it as an identity column. Any ideas on how to get around this?

It looks like this might be a limitation of the EF code migrations, as it does not handle identity columns properly. It works fine when I create a new column and mark it as identity, but seems to get confused when setting existing entities.

juharr
  • 31,741
  • 4
  • 58
  • 93
Atti
  • 380
  • 4
  • 21
  • 1
    See this http://stackoverflow.com/questions/1049210/adding-an-identity-to-an-existing-column – Steve Greene Sep 09 '15 at 14:56
  • Hi Steve thanks for the quick answer, I am looking at this from the EF-Migrations point of view the SQL needs to be correctly created by EF. Is there a way to overwrite the AlterColumn method for migrations in order to ensure it will generate the correct SQL. – Atti Sep 09 '15 at 15:35
  • You could just add the SQL needed to your Up() method using the Sql("ALTER TABLE ..."); etc. Then apply do the update-database. If you're going to ever revert back you'd need to write the reverse Down() code. – Steve Greene Sep 09 '15 at 16:14

0 Answers0