'PK_dbo.EntityName' is not a constraint. Could not drop constraint.EF6

code-first entity-framework-6

Question

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 ?

1
3
8/20/2016 8:17:49 PM

Popular Answer

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.

3
9/4/2016 2:28:10 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow