2014-10-11 6 views
6

Quando eseguo il seguente migrazione, sto ottenendo il seguente errore:L'istruzione ALTER TABLE conflitto con il vincolo FOREIGN KEY

The ALTER TABLE statement conflicted with the FOREIGN KEY constraint

ho un database esistente e refactoring del modello per includere una proprietà di navigazione.

vedere il modello originale e quindi il nuovo modello:

modello originale:

public class Student 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public string Country { get; set; } 
} 

Nuovo modello: proprietà

public class Student 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public int CountryID { get; set; } 
    public virtual Country Country { get; set; } 
} 

public class Country 
{ 
    public int ID { get; set; }    
    public string Country { get; set; } 
} 

Add-Migration navigazione:

public override void Up() 
{ 
      CreateTable(
       "dbo.Countries", 
       c => new 
        { 
         ID = c.Int(nullable: false, identity: true), 
         CountryName = c.String(), 
        }) 
       .PrimaryKey(t => t.ID); 

      AddColumn("dbo.Students", "CountryID", c => c.Int(nullable: false)); 
      CreateIndex("dbo.Students", "CountryID"); 
      AddForeignKey("dbo.Students", "CountryID", "dbo.Countries", "ID", cascadeDelete: true); 
      DropColumn("dbo.Students", "Country"); 
} 

Update-Database errore:

System.Data.SqlClient.SqlException (0x80131904): The ALTER TABLE statement conflicted with the FOREIGN KEY constraint "FK_dbo.Students_dbo.Countries_CountryID". The conflict occurred in database "aspnet-navprop-20141009041805", table "dbo.Countries", column 'ID'.

+0

Perché questa domanda è stata respinta? Pensavo che la domanda fosse ben documentata. –

risposta

3

Ho avuto lo stesso problema, la mia tabella aveva i dati quindi ho cambiato la colonna chiave esterna a nullable.

AddColumn("dbo.Students", "CountryID", c => c.Int(nullable: true)); 

Si dovrebbe modificare il codice del genere quindi eseguire nuovamente Update-Database -Verbose

public override void Up() 
{ 
      CreateTable(
       "dbo.Countries", 
       c => new 
        { 
         ID = c.Int(nullable: false, identity: true), 
         CountryName = c.String(), 
        }) 
       .PrimaryKey(t => t.ID); 

      AddColumn("dbo.Students", "CountryID", c => c.Int(nullable: true)); 
      CreateIndex("dbo.Students", "CountryID"); 
      AddForeignKey("dbo.Students", "CountryID", "dbo.Countries", "ID", cascadeDelete: true); 
      DropColumn("dbo.Students", "Country"); 
} 
3

Ho avuto questo stesso problema e troncato la tabella con i record di chiave esterna ed è riuscito.

2

Se nella tabella sono presenti dati (tabella degli studenti), eliminarli e riprovare di nuovo.

0

Contro la tua entità Student è possibile contrassegnare la vostra proprietà CountryId come annullabile con un punto di domanda allegato al tipo, ie

public class Student 
{ 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public int? CountryID { get; set; } 
    public virtual Country Country { get; set; } 
}