实体框架抛出异常 - 无效的对象名称dbo.BaseCs“ [英] Entity Framework throws exception - Invalid object name 'dbo.BaseCs'

查看:261
本文介绍了实体框架抛出异常 - 无效的对象名称dbo.BaseCs“的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我跟着亚当的回答<一个href=\"http://stackoverflow.com/questions/7781426/entity-framework-throws-exception-network-related-or-instance-specific-error-o\">here和实体框架现在工作和种子()方法也适用。

但是,当我尝试访问该数据库是这样的:

 公共用户FindUserByID(INT ID)
    {
        返回(从this.Users项目
                其中,item.ID == ID
                选择项目).SingleOrDefault();
    }
  .................................................. ...........................
    // GET:/主/    公众的ActionResult指数(INT?ID)
    {
        变种分贝= UserDataBaseDB.Create();        如果(!id.HasValue)
            ID = 0;        返回查看(db.FindUserByID(id.Value));
    }

它抛出一个异常,在在this.Users回报(从项目指出:


异常详细信息:System.Data.SqlClient.SqlException:无效的对象名称dbo.BaseCs


我试着更换它:
返回this.Users.ElementAt(ID); ,但随后引发此异常

LINQ到实体无​​法识别方法MySiteCreator.Models.User的ElementAt [用户](System.Linq.IQueryable 1 [MySiteCreator.Models.User]的Int32 )的方法,而这种方法不能被翻译成店前pression.`

谁能帮我?

谢谢!


解决方案

  

异常详细信息:System.Data.SqlClient.SqlException:无效的对象名称dbo.BaseCs


此错误意味着EF被翻译您的LINQ到使用命名的对象(最有可能是表) dbo.BaseCs ,它不会在存在一个sql语句数据库。

检查您的数据库,并验证该表是否存在,或者说,你应该使用不同的表名。另外,如果你可以发布一个链接到您遵循教程,这将有助于你在做什么跟着。


I've followed Adam's answer here and the Entity Framework now works and the Seed() method also works.

But when I try to access the database like this:

    public User FindUserByID(int id)
    {
        return (from item in this.Users
                where item.ID == id
                select item).SingleOrDefault();
    }
  .............................................................................
    // GET: /Main/

    public ActionResult Index(int? id)
    {
        var db = UserDataBaseDB.Create();

        if (!id.HasValue)
            id = 0;

        return View(db.FindUserByID(id.Value));
    }

It throws an exception at return (from item in this.Users stating:

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

I've tried replacing it with: return this.Users.ElementAt(id); but then it throws this exception.

LINQ to Entities does not recognize the method 'MySiteCreator.Models.User ElementAt[User](System.Linq.IQueryable1[MySiteCreator.Models.User], Int32)' method, and this method cannot be translated into a store expression.`

Can anyone help me?
Thank you!

解决方案

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

This error means that EF is translating your LINQ into a sql statement that uses an object (most likely a table) named dbo.BaseCs, which does not exist in the database.

Check your database and verify whether that table exists, or that you should be using a different table name. Also, if you could post a link to the tutorial you are following, it would help to follow along with what you are doing.

这篇关于实体框架抛出异常 - 无效的对象名称dbo.BaseCs“的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆