简体   繁体   中英

ASP.NET Core error occurred during publish and site is not working

In ASP.NET Core I added a new column to my database and now when I publish I get an error图片

I am trying to add CourierId to my Prescriptions table, it works in debug but fails to publish and my website is currently down as well. In the error it says "IF NOT EXISTS(SELECT*FROM[_EFMigrationHisto" and FK_Prescriptions_Courier_Courierid is not a valid contstraint. I am using SQL Here is the code for my Couriers table and Prescriptions table

public class Courier
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Prescription> Prescriptions { get; set; }
}
public class Prescription
{
    [Key]
    public int Id { get; set; }
    //[NotMapped]
    //public string Courier { get; set; }
    [Display(Name = "Courier")]
    public int? CourierId { get; set; }
    public virtual Courier Courier { get; set; }
}

How can I add the CourierId column to Prescriptions ? Publish and have the website work?

I found an answer thanks to the comments @Alexan, I have a new problem now but I will have to work on that outside of this post. My migration had a lot of extra code and after commenting it out, I published and it ran fine. Originally nothing was commented

protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AddColumn<int>(
            name: "CourierId",
            table: "Prescriptions",
            nullable: true);
        //migrationBuilder.DropForeignKey(
        //    name: "FK_Prescriptions_Couriers_CourierId",
        //    table: "Prescriptions");

        //migrationBuilder.AlterColumn<int>(
        //    name: "CourierId",
        //    table: "Prescriptions",
        //    nullable: true,
        //    oldClrType: typeof(int));

        //migrationBuilder.InsertData(
        //    table: "Couriers",
        //    columns: new[] { "Id", "Name" },
        //    values: new object[] { 1, "Akeem" });

        //migrationBuilder.InsertData(
        //    table: "Couriers",
        //    columns: new[] { "Id", "Name" },
        //    values: new object[] { 2, "Mitch" });

        //migrationBuilder.InsertData(
        //    table: "Couriers",
        //    columns: new[] { "Id", "Name" },
        //    values: new object[] { 3, "Devin" });

        //migrationBuilder.AddForeignKey(
        //    name: "FK_Prescriptions_Couriers_CourierId",
        //    table: "Prescriptions",
        //    column: "CourierId",
        //    principalTable: "Couriers",
        //    principalColumn: "Id",
        //    onDelete: ReferentialAction.Restrict);
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropColumn(
            name: "CourierId",
            table: "Prescriptions");
        //migrationBuilder.DropForeignKey(
        //    name: "FK_Prescriptions_Couriers_CourierId",
        //    table: "Prescriptions");

        //migrationBuilder.DeleteData(
        //    table: "Couriers",
        //    keyColumn: "Id",
        //    keyValue: 1);

        //migrationBuilder.DeleteData(
        //    table: "Couriers",
        //    keyColumn: "Id",
        //    keyValue: 2);

        //migrationBuilder.DeleteData(
        //    table: "Couriers",
        //    keyColumn: "Id",
        //    keyValue: 3);

        //migrationBuilder.AlterColumn<int>(
        //    name: "CourierId",
        //    table: "Prescriptions",
        //    nullable: false,
        //    oldClrType: typeof(int),
        //    oldNullable: true);

        //migrationBuilder.AddForeignKey(
        //    name: "FK_Prescriptions_Couriers_CourierId",
        //    table: "Prescriptions",
        //    column: "CourierId",
        //    principalTable: "Couriers",
        //    principalColumn: "Id",
        //    onDelete: ReferentialAction.Cascade);
    }
}

So now the problem is Courier table is not created but CourierId is added to Prescriptions table and it publishes, so now I think I will need to uncomment the migrationBuilder.InsertData but I will see.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM