简体   繁体   English

即使对象有效,对象名称也无效

[英]Invalid object name even though the object is valid

I have a unit test for a controllers actionresult. 我有一个针对控制器actionresult的单元测试。 When I run the unit test it breaks at db.SaveChanges(); 当我运行单元测试时,它在db.SaveChanges();db.SaveChanges(); part of the action result. 动作结果的一部分。 When I test the actual method (via web brower and data in database) it is fine however when I run my unit test it fails. 当我测试实际方法时(通过网络浏览器和数据库中的数据),这很好,但是当我运行单元测试时,它失败了。 The error I am receiving is: DbUpdateException was unhandled by user code. 我收到的错误是:用户代码未处理DbUpdateException。 See inner exception, Inner Exception: {"Invalid object name 'dbo.Projects'."} I am using entityframeworks codefirst structure and here is my code: 请参阅内部异常,内部异常: {"Invalid object name 'dbo.Projects'."}我正在使用entityframeworks codefirst结构,这是我的代码:

[ValidateAntiForgeryToken]
[Authorize(Roles = "A")]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddProject(Projects project)
{
   if (ModelState.IsValid)
   {
      using (var db = new MyDbContext())
      {
         db.ProjectModels.Add(project);
         db.SaveChanges();
      }
      return RedirectToAction("ListOfProjects", "Project");
   }
   return View(project);
}


[TestMethod()]
public void AddProjectTestWithModl()
{
    var controller = new AdminController();
    var model = new Projects() { Name = "Name", Description = "Desc", Duration = "1 month" };
    var result = controller.AddProject(model) as ViewResult;
    Assert.AreEqual(model, result.ViewData.Model);
}

Invalid object name 'dbo.Projects'. 无效的对象名称“ dbo.Projects”。 is an exception from sql server. 是SQL Server的例外。 It means your table does not exist in the dbo schema. 这意味着您的表在dbo模式中不存在。

Check that you are connected to the correct database and that migrations have been run. 检查您已连接到正确的数据库,并且已运行迁移。

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

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