简体   繁体   中英

Invalid object name using code first Entity Framework on database table

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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM