0

I first designed my database in SQL Server Management Studio.
Then used code to generate the entities.
For a table (say Product) the id column (say ProductID) is not set as identity, so in entity the id property (say ProductID) has an attribute like [DatabaseGenerated(DatabaseGeneratedOption.None)].
I changed it to [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
That was the only change I made to the entity.
Now I added a migration and then when I Update-Database, it throws an error

Error Number:3728,State:1,Class:16
'PK_dbo.****' is not a constraint.
Could not drop constraint. See previous errors.

Any idea on how to achieve this?

Amitava Karan
  • 637
  • 1
  • 5
  • 20

2 Answers2

1

You could either add the migration with a flag to tell EF not to generate the Up/Down code (Add-Migration MyMigration –IgnoreChanges) or you could simply comment out the generated Up() code and update-database. This will sync the model with the database.

Steve Greene
  • 12,029
  • 1
  • 33
  • 54
  • I think `Update-Database` command will sync the database with the model changes, will not change the model according to the database. I have tried. It did not work. – Amitava Karan Jun 18 '15 at 07:46
  • Sorry, thought you already had the column in the database set as identity. You need to get the sql side configured with identity before you do this. See the link Gert provided. – Steve Greene Jun 18 '15 at 13:41
0

when u create the migration on a table who has records, the foreign key that u are create has must be null