Amitava Karan
Amitava Karan

Reputation: 677

Entity Framework CodeFirst make primary key identity

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?

Upvotes: 0

Views: 1108

Answers (2)

Yoider Murillo
Yoider Murillo

Reputation: 1

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

Upvotes: 0

Steve Greene
Steve Greene

Reputation: 12304

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.

Upvotes: 1

Related Questions