''PK_dbo.EntityName' is not a constraint. Could not drop constraint.EF6
I'm using Entity Framework 6 code first. I have three Entity like these :
public class Doctor
{
public string DoctorID { get; set; }
public string firstName { get; set; }
public string lastName { get; set; }
}
public class ExpertiseDetails
{
[Key, Column(Order = 1)]
public short expertiseID { get; set; }
[Key , Column(Order = 2)]
public string DoctorID { get; set; }
[ForeignKey("expertiseID")]
public Expertise expertise { get; set; }
public Doctor doctor { get; set; }
}
public class Expertise
{
[Key]
public short expertiseID { get; set; }
public string expertiseTitle { get; set; }
}
I need a one to many realationship between Expertise and Doctor,When I run update-database statement in console nuGet this error shows :
'PK_dbo.ExpertiseDetails' is not a constraint. Could not drop constraint
What's wrong ?
Solution 1:[1]
Having experienced a similar issue after renaming the table schema, I solved by explicitly delete the PK name with sql.
In my case the issue arose when a primary key was changed in a table after renaming the table schema:
Original:
- dbo.LogEntries with PK-name: PK_dbo.LogEntries
step 1: schema change:
- Logging.LogEntries with PK unchanged
step 2: PK change
- PK changed: results in
DropPrimaryKey("Logging.LogEntries");
This last line translates to the PK-name: PK_Logging.LogEntries which doesn't exists.
The fix: In general there are several way's to fix this. I dropped the PK through a sql-statement in the explicit migration.
Sql("ALTER TABLE [Logging].[LogEntries] DROP CONSTRAINT [PK_dbo.LogEntries]");
Be advised; I had full production access in case it would fail and didn't use migrations to roll-back to a previous state.
Solution 2:[2]
As mentioned here the default names may not match your database. In this case, the documentation says to manually modify your migration file (Up and Down methods) to make use of the name parameter, which will override the default name of the primary or foreign key (or other database object depending on the situation).
For example, for me, the generated line:
DropPrimaryKey("dbo.MyTable");
becomes:
DropPrimaryKey("dbo.MyTable", "PK_MyTable");
Solution 3:[3]
You can rename the primary key to the name the entity framework asking about.
For example, if an error message mentioned 'PK_dbo.TableName' and the existing primary key does not have 'dbo.', add 'dbo.' to it's name and run the application or update the database with nuget package manager console.
Solution 4:[4]
Add in your component file this (above your component right after imports or in the store file and export it):
type RootState = ReturnType<typeof store.getState>;
and then use it like this:
const quantity = useSelector((state: RootState)=>state.quantity)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Stefan |
| Solution 2 | Dave Cousineau |
| Solution 3 | CyanCoding |
| Solution 4 |
