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

查看:22
本文介绍了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"的表开始收集数据,我需要能够将其重命名为Courses"而不影响数据,但目前以下是迁移中生成的 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/en-us/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天全站免登陆