Jimmyt1988
Jimmyt1988

Reputation: 21136

Change table name related to entity model

I have some data in my database in a table called "Test". I've made a duplicate of that table called "CopyTest".

If i change:

modelBuilder.Entity<IISLog>()
    .ToTable("Test");

to:

modelBuilder.Entity<IISLog>()
    .ToTable("CopyTest");

I get an error saying:

context has changed since the database was created. Consider using Code First Migrations to update the database

How can i stop this from showing? It's just a table name change :)

Upvotes: 1

Views: 35

Answers (3)

Alex
Alex

Reputation: 38519

Add a new migration using add-migration Rename_Test_To_CopyTest

Then, in the resulting file, use the RenameTable method:

public partial class Rename_Test_To_CopyTest : DbMigration
{
    public override void Up()
    {
        RenameTable("dbo.Test", "dbo.CopyTest");
    }

    public override void Down()
    {
        RenameTable("dbo.CopyTest", "dbo.Test");
    }
}

Then use update-database as usual.

Upvotes: 0

osanger
osanger

Reputation: 2352

Migrate to the initial DB with

Update-Database -TargetMigration:"name_of_migration"

then update to your current state:

Update-Database 

if necessary add your migration again:

Add-Migration TableNameUpdate

Upvotes: 1

Utkarsh Dubey
Utkarsh Dubey

Reputation: 776

  • Solution 1:

Delete First Migration and than try:

Add-Migration <migration-name>

Then open Migration file and change Table Name Manually. After that:

update-database -verbose

If it not works.

  • Solution 2:

Try to delete Migration History from SQL server Management studio .

Reference URL - Resetting Entity Framework Migrations to a clean Slate

Upvotes: 1

Related Questions