12

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.

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.

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"

...and to update database

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

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);
            });

    }

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?

Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17
James Morrison
  • 1,954
  • 2
  • 21
  • 48
  • 13
    If you want to just rename the **table**, simply use `[Table]` data annotation or `ToTable` fluent API. If you indeed want to rename the **entity class**, then see [Rename a foreign key in Entity Framework Core without dropping data](https://stackoverflow.com/questions/49799627/rename-a-foreign-key-in-entity-framework-core-without-dropping-data/49802766#49802766) (the title is misleading). – Ivan Stoev Jul 26 '18 at 12:40
  • 2
    Issue [#5826](https://github.com/aspnet/EntityFrameworkCore/issues/5826) will fix this for most cases. – bricelam Jul 26 '18 at 17:01
  • 1
    @IvanStoev thanks, that could be a good workaround for now, all the editing and code generation is done through the Roslyn Api so will need to hold out for the issue bricelam posted to be resolved I think. Thanks guys – James Morrison Jul 27 '18 at 08:35
  • Possible duplicate of [Entity Framework Migrations renaming tables and columns](https://stackoverflow.com/questions/13296996/entity-framework-migrations-renaming-tables-and-columns) – Jim G. Jul 03 '19 at 13:04
  • @JimG. EF core is an entirely different build to EF 5, this is also an entirely different issue – James Morrison Jul 03 '19 at 13:48

2 Answers2

6

Check out https://learn.microsoft.com/en-us/dotnet/api/microsoft.entityframeworkcore.migrations.migrationbuilder.renametable?view=efcore-2.1

You can just call

migrationBuilder.RenameTable("Old", null, "New");
Jim G.
  • 15,141
  • 22
  • 103
  • 166
  • 7
    For those that haven't check @Ivan Stoev comment, please do so. It is the best answer, special if you have others relations, constrains and indexes. – bruno.almeida Jul 29 '19 at 15:35
0

I was encountering the same issue in EF Core 5.0 with the dotnet ef migrations add command. It wanted to drop tables and recreate them, meaning I would lose all my data.

I was trying to add a new table and rename a few of them. It didn't like that apparently and I split it out in 2 migrations. Firstly I added the new table and then I renamed the other tables.

DevNebulae
  • 4,566
  • 3
  • 16
  • 27