ADP_X
ADP_X

Reputation: 333

ASP.NET MVC Code First Data migrations with renamed database tables

I am developing a ASP.NET MVC web application using code first with entity framework.

The problem I am facing is that I've made a rename of a couple of tables using data migrations and apparently everything went fine I even see the table names updated in the database.

But when I create a new data migration it always takes the original name of the table (xxxxes), not the latest (xxxxs) and I have to change manually the migration script to avoid the error:

Cannot find the object "dbo.xxxxs" because it does not exist or you do not have permissions.

And what is worse, the index controller now fails because when it tries the ToList() method it throws the following error:

An exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll but was not handled in user code Additional information: An error occurred while executing the command definition. See the inner exception for details.

Being inner details:

{"Invalid object name 'dbo.xxxxs'."}

I've looked in the whole project to try to find the place where this tables are still referenced with the original name but I've found nothing.

Any idea about how to solve this??

EDIT

What I did, and maybe here is where everything broke is create a data migration like this and run it.

public partial class Update5 : DbMigration
{
    public override void Up()
    {
        RenameTable("xxxxes", "xxxxs");
    }

    public override void Down()
    {

    }
}

I've seen that the Down() method should have just the opposite action, which I did not add...

Upvotes: 1

Views: 110

Answers (1)

Stef Geysels
Stef Geysels

Reputation: 1047

You can try to add the table names via data-annotations. Check this link

By default, EF will use the name of your class to name the table. If you changed the table name, EF will not know which class corresponds with which table. So if you add the name of the table with an annotation above the class, EF will know what to find.

Upvotes: 1

Related Questions