【发布时间】:2015-11-12 06:37:56
【问题描述】:
我收到了错误Invalid object name 'dbo.Staffs'.,但我不知道为什么。实际上,我使用 EF 删除并重新创建了我的数据库,因为之前我遇到了其他错误。但我很确定我正确地重新创建了它,因为我已经为其他程序以相同的方式完成了它并且它工作正常。
控制器
private StaffPortalDBEntities1 db = new StaffPortalDBEntities1();
SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["StaffPortalDBConnectionString"].ConnectionString);
[Authorize]
public ActionResult Index()
{
var userEmail = User.Identity.Name;
var model = db.Staffs.Where(i => i.Email == userEmail).Include("Histories").Include("CurrentApplications").FirstOrDefault();
return View(model);
}
我得到的错误是var model = db.Staffs.Where(i => i.Email == userEmail).Include("Histories").Include("CurrentApplications").FirstOrDefault();这一行
生成的 Staff 类
public partial class Staff
{
public Staff()
{
this.Histories = new HashSet<History>();
this.CurrentApplications = new HashSet<CurrentApplication>();
}
public int StaffID { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public Nullable<decimal> AllocatedLeave { get; set; }
public Nullable<int> BalanceLeave { get; set; }
public virtual ICollection<History> Histories { get; set; }
public virtual ICollection<CurrentApplication> CurrentApplications { get; set; }
}
【问题讨论】:
-
您的复数约定似乎有些问题。尝试在
Staff类上方添加[Table(Name = "Staff")]属性。 -
@StuartLC 嗨,我试过了,但我仍然收到同样的错误 D:
-
看起来您在某处歪曲了模型创建,智能感知是否为您提供了人员?你检查过模型吗?为什么要在当前应用程序的主键上使用这么多属性?
标签: sql-server asp.net-mvc entity-framework object