Die ALTER TABLE-Anweisung in Konflikt mit der FOREIGN KEY-Einschränkung

Wenn ich führen Sie die folgenden Migrations -, ich erhalte die folgende Fehlermeldung:

Die ALTER TABLE-Anweisung in Konflikt mit der FOREIGN KEY-Einschränkung

Ich habe eine vorhandene Datenbank und die Umgestaltung der Modell, um eine navigation-Eigenschaft.

Sehen, das ursprüngliche Modell und dann das neue Modell:

Original Modell:

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

Neues Modell:

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 navigation Immobilie:

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 Fehler:

System.Daten.SqlClient.SqlException (0x80131904): Die ALTER TABLE-Anweisung in Konflikt mit der FOREIGN KEY-Einschränkung "FK_dbo.Students_dbo.Countries_CountryID". Der Konflikt ist aufgetreten in Datenbank "aspnet-navprop-20141009041805", Tabelle "dbo.Ländern", Spalte 'ID'.

Warum wurde diese Frage abgestimmt? Ich dachte, die Frage war gut dokumentiert.

InformationsquelleAutor Barry MSIH | 2014-10-11

Schreibe einen Kommentar