I am working on a DB-first environment with a .NET application.
I realized when I create migration files, the entity framework does not pick up the correct primary key name from the SQL server. When it needs to drop a primary key.
What migration file generates to drop the primary key DropPrimaryKey("dbo.TableName");
But the SQL server has PK constraint like below [PK__TableName__48AFA797B7F88EC3]
I have two questions,
- Why PK has these randomly generated numbers in the name?
- Why entity framework can not detect that name and create a migration file according to the current schema?
When I run the migration file contains DropPrimaryKey("dbo.TableName");
it doesn't work and gives me the error below.
'PK_dbo.TableName' is not a constraint.
Could not drop constraint. See previous errors.
CodePudding user response:
Why PK has these randomly generated numbers in the name?
If you don't name the constraint, SQL Server will generate a name for it.
When I run the migration file . . .
Why are you even generating migrations? In a database-first workflow you apply schema changes directly to the database and then re-scaffold or adjust the EF model to match. Migrations are only used in a code-first workflow.