繁体   English   中英

发布期间发生 ASP.NET Core 错误并且站点无法正常工作

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

在 ASP.NET Core 中,我向数据库添加了一个新列,现在发布时出现错误图片

我正在尝试将CourierId添加到我的Prescriptions表中,它在调试中工作但无法发布,我的网站目前也已关闭。 在错误中,它说"IF NOT EXISTS(SELECT*FROM[_EFMigrationHisto"FK_Prescriptions_Courier_Courierid is not a valid contstraint.我正在使用 SQL 这是我的Couriers表和Prescriptions表的代码

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; }
}

如何将CourierId列添加到Prescriptions 发布并让网站正常工作?

感谢@Alexan 的评论,我找到了答案,我现在有一个新问题,但我将不得不在这篇文章之外解决这个问题。 我的迁移有很多额外的代码,在将其注释掉后,我发布了并且运行良好。 原本什么都没评论

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);
    }
}

所以现在问题是没有创建Courier表,但CourierId被添加到Prescriptions表并发布,所以现在我想我需要取消对migrationBuilder.InsertData注释,但我会看到。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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