NuGet 更新数据库未在 Visual Studio 中提取新记录

NuGet Update-Database is not pulling new records in Visual Studio

VisualStudio 和 NuGet 的新手,所以我正在寻找一些指导。

我在一个团队项目中使用 TFS 并使用 NuGet 获取该项目数据库的更新。 我的一位团队成员在 属性 中添加了一个列到我们的一个数据库中,然后还向该数据库中添加了一条新记录。然后他为那个实例创建了一个新的迁移。

我正在尝试将其更新到我们其余的 VisualStudio 设置中。 到目前为止,根据我的研究,我在 NuGet 包管理器中 运行 命令 Update-Database 。这更新了数据库的结构并引入了新列但未能引入新记录。

我们已经尝试了 Update-Database -force,但一无所获。

我曾尝试寻找解决方案,但很难找到有关在数据库中迁移数据的信息。

有没有人对我如何在以后的许多更新中轻松地进行此操作有任何建议。

首先,欢迎来到SO!

This updated the structure of the database and brought in the new column but failed to bring in the new record.

创建迁移时,EF 会自动添加结构更改,但不会对数据执行相同的操作。如果您考虑一下,这是有道理的,因为 EF 无法知道在您的数据库中添加、修改或删除了哪些记录,而且 10 次中有 9 次我们不希望该数据出现在迁移脚本中。

如果您想在迁移中包含记录,您有两种选择...

1.为数据库播种

每次迁移都会调用此方法

internal sealed class Configuration : DbMigrationsConfiguration<WebApplication15.DatabaseContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }

    protected override void Seed(WebApplication15.DatabaseContext context)
    {
        //
        // Add records into the database
        //

    }
}

2。将数据添加到迁移

public partial class Initial : DbMigration
{
    public override void Up()
    {
        CreateTable(
            "dbo.People",
            c => new
                {
                    Id = c.Int(nullable: false, identity: true),
                })
            .PrimaryKey(t => t.Id);

        Sql("INSERT INTO People....");

    }

    public override void Down()
    {
        DropTable("dbo.People");
    }
}

We have tried Update-Database -force and that came back with nothing.

force 标志只是告诉 EF 迁移数据库,不管它是否会导致数据丢失(即删除一个充满记录的 table!)。

希望对您有所帮助!