![](/img/trans.png)
[英]Asp.net core in docker Unhandled exception. SqlException (0x80131904): .., but then an error occurred during the pre-login handshake
[英]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.