Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server 发布期间发生ASP.NET核心错误,网站不工作_Sql Server_Asp.net Core - Fatal编程技术网

Sql server 发布期间发生ASP.NET核心错误,网站不工作

Sql server 发布期间发生ASP.NET核心错误,网站不工作,sql-server,asp.net-core,Sql Server,Asp.net Core,在ASP.NET Core中,我向数据库添加了一个新列,现在发布时出现了一个错误 我正在尝试将CourierId添加到我的处方表中,它在调试中工作,但无法发布,我的网站目前也已关闭。在错误中,它说“如果不存在(从[\u EFMigrationHisto”和FK\u处方\u Courier\u Courierid不是有效的contstraint.我在使用SQL这里是我的信使的代码表和处方表 public class Courier { [Key] public int Id {

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

我正在尝试将
CourierId
添加到我的
处方
表中,它在调试中工作,但无法发布,我的网站目前也已关闭。在错误中,它说
“如果不存在(从[\u EFMigrationHisto”
FK\u处方\u Courier\u Courierid不是有效的contstraint.
我在使用SQL这里是我的
信使的代码
表和
处方

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; }
}
公共类快递
{
[关键]
公共int Id{get;set;}
公共字符串名称{get;set;}
公共i收集处方{get;set;}
}
公营处方
{
[关键]
公共int Id{get;set;}
//[未映射]
//公共字符串信使{get;set;}
[显示(Name=“Courier”)]
公共int?CourierId{get;set;}
公共虚拟信使信使{get;set;}
}

如何将
CourierId
列添加到
处方
?发布并使网站正常运行?

我通过评论@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);
    }
}
protected override void Up(MigrationBuilder MigrationBuilder)
{
migrationBuilder.AddColumn(
姓名:“信使”,
表:“处方”,
可为空:真);
//migrationBuilder.DropForeignKey(
//名称:“FK\U处方\快递员\快递员”,
//表:“处方”);
//migrationBuilder.AlterColumn(
//姓名:“信使”,
//表:“处方”,
//可为空:是的,
//oldcltype:typeof(int));
//migrationBuilder.InsertData(
//表:“信使”,
//列:新[]{“Id”,“Name”},
//值:新对象[]{1,“Akeem”});
//migrationBuilder.InsertData(
//表:“信使”,
//列:新[]{“Id”,“Name”},
//值:新对象[]{2,“Mitch”});
//migrationBuilder.InsertData(
//表:“信使”,
//列:新[]{“Id”,“Name”},
//值:新对象[]{3,“Devin”});
//migrationBuilder.AddForeignKey(
//名称:“FK\U处方\快递员\快递员”,
//表:“处方”,
//专栏:“信使”,
//原则:“信使”,
//主栏:“Id”,
//onDelete:referentialiction.Restrict);
}
受保护的覆盖无效关闭(MigrationBuilder MigrationBuilder)
{
migrationBuilder.DropColumn(
姓名:“信使”,
表:“处方”);
//migrationBuilder.DropForeignKey(
//名称:“FK\U处方\快递员\快递员”,
//表:“处方”);
//migrationBuilder.DeleteData(
//表:“信使”,
//keyColumn:“Id”,
//键值:1);
//migrationBuilder.DeleteData(
//表:“信使”,
//keyColumn:“Id”,
//键值:2);
//migrationBuilder.DeleteData(
//表:“信使”,
//keyColumn:“Id”,
//键值:3);
//migrationBuilder.AlterColumn(
//姓名:“信使”,
//表:“处方”,
//可为空:false,
//oldClrType:typeof(int),
//oldNullable:true);
//migrationBuilder.AddForeignKey(
//名称:“FK\U处方\快递员\快递员”,
//表:“处方”,
//专栏:“信使”,
//原则:“信使”,
//主栏:“Id”,
//onDelete:引用。级联);
}
}

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

你做了迁移吗?@Alexan是的,我做了迁移并更新了数据库,它说
>字符串参数“migrationId”不能为空
我尝试多次后会得到一个“sqlexception invalid columnId”,我进入sql server并手动添加了表和列,一切正常,但现在当我发布此事件时,基本上更新数据库说
字符串参数“MigarionId”不能为空
@alexan谢谢你,但是如果我确实删除了开发数据库,那到底是做什么的呢?听起来怎么样?我所有的测试数据都会被删除吗?还是类似于“删除并重新创建数据”?我假设它也不会影响我数据库上的实际数据