幻像列:实体框架迁移

时间:2018-04-09 13:55:15

标签: sql asp.net-mvc entity-framework ef-code-first ef-migrations

在更新当前项目的数据库结构的过程中,我向链接表添加了一个依赖关系,这样关系就是:

JobOffer -< JobApplication >- StaffMember

将此更改添加为新迁移时,添加了其他几个字段&#34;迁移Up()Down()方法。我不再拥有确切的字段,但它们与Skills.JobOffer_ID一致。 添加的原因是引用是因为我从未运行update-database,而是删除了这些不必要的字段,然后运行update-database

更新似乎工作,并添加了正确的字段,但在运行代码时,我收到此错误: SQL Exception referencing non-existant columns

列不在数据库中,但我不知道为什么它们首先被引用,它们从未被创建过,也从未在任何代码中明确写入。

如何防止引用不存在列的这些错误?

修改

目前的模型如下,与上一个模型的唯一变化是将所有者ID添加到JobOpportunity。

Database Logical model

当前的迁移如下(减去我删除的字段,确定它们是什么)

public partial class JobChanges : DbMigration
{
    public override void Up()
    {
        AddColumn("dbo.JobOpportunities", "Name", c => c.String());
        AddColumn("dbo.JobOpportunities", "OwnerID", c => c.Int());
        CreateIndex("dbo.JobOpportunities", "OwnerID");
        AddForeignKey("dbo.JobOpportunities", "OwnerID", "dbo.Staff", "ID", cascadeDelete: true);
    }

    public override void Down()
    {
        DropForeignKey("dbo.JobOpportunities", "OwnerID", "dbo.Staff");
        DropIndex("dbo.JobOpportunities", new[] { "OwnerID" });
        DropColumn("dbo.JobOpportunities", "OwnerID");
        DropColumn("dbo.JobOpportunities", "Name");
    }
}

编辑2:

刚刚意识到数据库atm中只存在JobOpportunities,Staff和Skills #facepalm#

编辑3:

重新修复了错误的迁移,这就是我的开始:

public partial class JobChanges : DbMigration
{
    public override void Up()
    {
        AddColumn("dbo.JobOpportunities", "Name", c => c.String());
        AddColumn("dbo.JobOpportunities", "OwnerID", c => c.Int(nullable: false));
        AddColumn("dbo.JobOpportunities", "Staff_ID", c => c.Int());
        AddColumn("dbo.Skills", "Staff_ID", c => c.Int());
        AddColumn("dbo.Skills", "JobOpportunity_ID", c => c.Int());
        AddColumn("dbo.Skills", "JobOpportunity_ID1", c => c.Int());
        AddColumn("dbo.Staff", "JobOpportunity_ID", c => c.Int());
        CreateIndex("dbo.JobOpportunities", "OwnerID");
        CreateIndex("dbo.JobOpportunities", "Staff_ID");
        CreateIndex("dbo.Staff", "JobOpportunity_ID");
        CreateIndex("dbo.Skills", "Staff_ID");
        CreateIndex("dbo.Skills", "JobOpportunity_ID");
        CreateIndex("dbo.Skills", "JobOpportunity_ID1");
        AddForeignKey("dbo.JobOpportunities", "Staff_ID", "dbo.Staff", "ID");
        AddForeignKey("dbo.Skills", "Staff_ID", "dbo.Staff", "ID");
        AddForeignKey("dbo.Staff", "JobOpportunity_ID", "dbo.JobOpportunities", "ID");
        AddForeignKey("dbo.Skills", "JobOpportunity_ID", "dbo.JobOpportunities", "ID");
        AddForeignKey("dbo.JobOpportunities", "OwnerID", "dbo.Staff", "ID", cascadeDelete: true);
        AddForeignKey("dbo.Skills", "JobOpportunity_ID1", "dbo.JobOpportunities", "ID");
    }

    public override void Down()
    {
        DropForeignKey("dbo.Skills", "JobOpportunity_ID1", "dbo.JobOpportunities");
        DropForeignKey("dbo.JobOpportunities", "OwnerID", "dbo.Staff");
        DropForeignKey("dbo.Skills", "JobOpportunity_ID", "dbo.JobOpportunities");
        DropForeignKey("dbo.Staff", "JobOpportunity_ID", "dbo.JobOpportunities");
        DropForeignKey("dbo.Skills", "Staff_ID", "dbo.Staff");
        DropForeignKey("dbo.JobOpportunities", "Staff_ID", "dbo.Staff");
        DropIndex("dbo.Skills", new[] { "JobOpportunity_ID1" });
        DropIndex("dbo.Skills", new[] { "JobOpportunity_ID" });
        DropIndex("dbo.Skills", new[] { "Staff_ID" });
        DropIndex("dbo.Staff", new[] { "JobOpportunity_ID" });
        DropIndex("dbo.JobOpportunities", new[] { "Staff_ID" });
        DropIndex("dbo.JobOpportunities", new[] { "OwnerID" });
        DropColumn("dbo.Staff", "JobOpportunity_ID");
        DropColumn("dbo.Skills", "JobOpportunity_ID1");
        DropColumn("dbo.Skills", "JobOpportunity_ID");
        DropColumn("dbo.Skills", "Staff_ID");
        DropColumn("dbo.JobOpportunities", "Staff_ID");
        DropColumn("dbo.JobOpportunities", "OwnerID");
        DropColumn("dbo.JobOpportunities", "Name");
    }
}

0 个答案:

没有答案