Reputation: 399
Using Entity Framework Core Code-First development:
In the last migration I ran, I switched a relationship from (simplified)
public class SiteUser
{
public int Id {get; set;}
public int EmployeeId { get; set; }
[InverseProperty("SiteUser")]
public virtual Employee Employee { get; set; }
}
public class Employee
{
public int Id {get; set;}
[InverseProperty("Employee")]
public virtual SiteUser SiteUser { get; set; }
}
(which implies that SiteUser is a subset of Employee, opposite from the intended behavior) to
public class SiteUser
{
public int Id { get; set; }
}
public class Employee
{
public int Id {get; set;}
public int SiteUserId { get; set; }
[ForeignKey("SiteUserId")]
[InverseProperty("Employee")]
public virtual SiteUser SiteUser { get; set; }
}
This of course gave me a migration that, among other things, dropped a foreign key constraint:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_SiteUser_Employee_EmployeeId",
table: "SiteUser");
//etc
}
I ran that migration and updated the database from it, and it worked fine. Tables and foreign key constraints were correctly updated to the new model. However, I'm trying to do a new migration now, and when I try to run Update-Database, I get
System.Data.SqlClient.SqlException (0x80131904): 'FK_SiteUser_Employee_EmployeeId' is not a constraint.
Could not drop constraint. See previous errors.
My reaction is, well, duh EF, you dropped that constraint in the last migration; why are you trying to drop it again?
The new migration C# file doesn't have anything in it about dropping that constraint, so why is it trying? And how can I tell EF to stop crashing on this DROP CONSTRAINT line?
Upvotes: 3
Views: 6047