Entity Framework Core 2.1,使用代码优先迁移重命名表,而无需删除和创建新表 [英] Entity Framework Core 2.1, rename table using code-first migrations without dropping and creating new table

查看:132
本文介绍了Entity Framework Core 2.1,使用代码优先迁移重命名表,而无需删除和创建新表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将netcoreapp 2.1与EF Core 2.1结合使用,并通过数据迁移来更新数据库,但是在重命名表时遇到了问题。我这里的主要问题是一个表(以及以后可能的列)可能会多次使用数据重命名,我希望能够重命名它们,同时保持数据完整,但据我看来,这些迁移似乎只涉及保持

I'm using netcoreapp 2.1 with EF Core 2.1 and updating my database with data migrations and have come into a problem with renaming tables. My main issue here is a table (and later potentially columns) may be renamed multiple times with data and I want to be able to rename them while keeping this data intact but from what I've read it seems these migrations only seem concerned with keeping the schema up to date.

此问题类似于使用Entity Framework Core 2.0更改或重命名列名而不会丢失数据,但是由于我的流程是自动化的,所以我需要能够使用dotnet.exe在命令行上使用迁移本身来执行此操作。

This issue is similar to Change or rename a column name without losing data with Entity Framework Core 2.0 but as my process is automated I need to be able to do this using the migration itself on the command line with dotnet.exe.

为此,我将下面的参数传递给dotnet.exe,构建解决方案,从DLL获取数据库上下文,然后使用下面的代码行进行迁移。

To do this I am passing the argument below to dotnet.exe, building the solution, getting the DB context from the DLL and then running the migration with the lines below that.

ef migrations add "someMigrationName"

...并更新数据库

var migrator = dbContext.Database.GetService<IMigrator>();
migrator.Migrate();

例如,如果名为 Courases的表开始收集数据,我需要能够重命名它可以迁移而不影响数据,但是当前下面是迁移中生成的Up函数。

As an example, if a table named "Courases" starts collecting data I need to be able to rename it "Courses" without it affecting the data however currently the below is the generated Up function in the migration.

protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropTable(
            name: "Courases");

        migrationBuilder.CreateTable(
            name: "Courses",
            columns: table => new
            {
                Id = table.Column<Guid>(nullable: false),
                ConcurrencyCheck = table.Column<byte[]>(nullable: true)
            },
            constraints: table =>
            {
                table.PrimaryKey("PK_Courses", x => x.Id);
            });

    }

从我读到的内容来看,似乎没有办法用重命名的表而不是删除并重新创建表来生成迁移,但这似乎很疯狂,有没有办法做到/是否可以将我错过的标志传递给dotnet.exe?

From what I've read there seems to be no way to generate a migration with tables renamed rather than dropped and recreated but this seems crazy, is there a way of doing this/is there a flag I can pass to dotnet.exe that I've missed?

推荐答案

查看 https://docs.microsoft.com/zh-cn/dotnet/api/microsoft.entityframeworkcore.migrations.migrationbuilder.renametable?view=efcore-2.1

您只需调用migrationBuilder.RenameTable( Old,null, New);

You can just call migrationBuilder.RenameTable("Old", null, "New");

这篇关于Entity Framework Core 2.1,使用代码优先迁移重命名表,而无需删除和创建新表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆