实体框架引发异常 - 无效的对象名称“dbo.BaseCs" [英] Entity Framework throws exception - Invalid object name 'dbo.BaseCs'
问题描述
我已经按照亚当的回答 这里 和实体框架现在可以工作,Seed()
方法也可以工作.
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));
}
它在 return 时抛出异常(来自 this.Users 中的 item
说明:异常详细信息:System.Data.SqlClient.SqlException:对象名称dbo.BaseCs"无效.
It throws an exception at return (from item in this.Users
stating:
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.BaseCs'.
我尝试将其替换为:return this.Users.ElementAt(id);
但是它会抛出这个异常.
I've tried replacing it with:
return this.Users.ElementAt(id);
but then it throws this exception.
LINQ to Entities 无法识别方法 'MySiteCreator.Models.User ElementAt[User](System.Linq.IQueryable
1[MySiteCreator.Models.User], Int32)'方法无法转换为存储表达式.`
LINQ to Entities does not recognize the method 'MySiteCreator.Models.User ElementAt[User](System.Linq.IQueryable
1[MySiteCreator.Models.User], Int32)' method, and this method cannot be translated into a store expression.`
谁能帮帮我?
谢谢!
Can anyone help me?
Thank you!
推荐答案
异常详细信息:System.Data.SqlClient.SqlException:对象名称dbo.BaseCs"无效
Exception Details: System.Data.SqlClient.SqlException: Invalid object name 'dbo.BaseCs'
此错误意味着 EF 正在将您的 LINQ 转换为使用名为 dbo.BaseCs
的对象(很可能是表)的 sql 语句,该对象在数据库中不存在.
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屋!