0

I am using Entity Framework code-first migrations. And I created a migration to change the name of a table in my database:

public partial class ChangeNameOfCodePerformanceAnchorTable : DbMigration
{
    public override void Up()
    {
        RenameTable(name: "dbo.Code_PerformanceAnchor", newName: "CodePerformanceAnchor");
    }
    
    public override void Down()
    {
        RenameTable(name: "dbo.CodePerformanceAnchors", newName: "Code_PerformanceAnchor");
    }
}

Now, when I create the controller for that table.. on the Index action I have this:

public ActionResult Index()
{
    return View(db.CodePerformanceAnchor.Include(c => c.CodePhase).ToList());
}

When I run the application to get to that specific index page, I get this error:

Invalid object name 'dbo.CodePerformanceAnchors'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.CodePerformanceAnchors'.

The only thing in my application that is called CodePerformanceAnchors is the controller. The model is CodePerformanceAnchor.

public virtual DbSet<CodePerformanceAnchor> CodePerformanceAnchor { get; set; }

Any help on resolving this error would be greatly appreciated.

Community
  • 1
  • 1
Grizzly
  • 5,873
  • 8
  • 56
  • 109
  • [Pluralization](https://edspencer.me.uk/2012/03/13/entity-framework-plural-and-singular-table-names/) is the default. Check the actual name of the table in the database. – Steve Greene Mar 07 '19 at 19:22

1 Answers1

0

You have 2 names for table:

CodePerformanceAnchor
and
CodePerformanceAnchors (additional s at the end)

You can also check this (Pluggable Conventions) or this (specifying table name)

Pablo notPicasso
  • 3,031
  • 3
  • 17
  • 22
  • Sorry, I'm confused.. how do I fix it? – Grizzly Mar 07 '19 at 14:17
  • Have you changed `ChangeNameOfCodePerformanceAnchorTable` since creating it? Up method uses `CodePerformanceAnchor` why Down method uses `CodePerformanceAnchors`. Erorr seems to be thrown when Down is invoked. – Pablo notPicasso Mar 07 '19 at 14:19