简体   繁体   English

在数据库表上使用代码优先实体框架的无效对象名称

[英]Invalid object name using code first Entity Framework on database table

I am using Entity Framework code-first migrations. 我正在使用Entity Framework代码优先迁移。 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: 现在,当我为该表创建控制器时。在Index动作中,我有以下内容:

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'. 无效的对象名称“ dbo.CodePerformanceAnchors”。

Description: An unhandled exception occurred during the execution of the current web request. 描述:执行当前Web请求期间发生未处理的异常。 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'. 异常详细信息:System.Data.SqlClient.SqlException:无效的对象名称'dbo.CodePerformanceAnchors'。

The only thing in my application that is called CodePerformanceAnchors is the controller. 在我的应用程序中,唯一称为CodePerformanceAnchors是控制器。 The model is CodePerformanceAnchor . 该模型是CodePerformanceAnchor

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

Any help on resolving this error would be greatly appreciated. 解决此错误的任何帮助将不胜感激。

You have 2 names for table: 您有2个表格名称:

CodePerformanceAnchor CodePerformanceAnchor
and
CodePerformanceAnchors (additional s at the end) CodePerformanceAnchors(末尾为s)

You can also check this (Pluggable Conventions) or this (specifying table name) 您还可以检查 (可插拔约定)或 (指定表名)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Entity Framework代码优先迁移来更改数据库表名称 - Change a database table name using Entity Framework code first migrations 实体框架-首先从数据库“无效的列名&#39;discriminator&#39;&#39;开始编码 - Entity Framework - Code first from database “invalid column name 'discriminator'” 代码优先实体框架:外键 - 无效的对象名称 - Code-first Entity Framework : foreign key - invalid object name 如何使用数据库优先实体框架更改表名 - How to change table name using database first Entity Framework 如何首先使用 Entity Framework 6 代码将 object 作为字符串存储在数据库中 - How to store an object as a string in database using Entity Framework 6 code first 实体框架代码优先 - 更改表名称 - Entity Framework Code First - Changing a Table Name 首先在实体框架代码中初始化数据库中的对象 - Initialize object in database in Entity Framework Code First 在实体框架中使用数据库优先实体和代码优先实体 - Using database first entity with code first entity in entity framework 代码优先方法中使用Entity Framework的ForeignKeyAttribute名称无效 - Invalid ForeignKeyAttribute name with Entity Framework in code first approach 使用实体框架代码优先的ASP.NET MVC 3 Web应用程序上的列名ProveedoresID和UsuarioID无效 - Invalid column name ProveedoresID and UsuarioID on ASP.NET MVC 3 web app using Entity Framework Code First
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM