简体   繁体   中英

Invalid object name even though the object is valid

I have a unit test for a controllers actionresult. When I run the unit test it breaks at 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. See inner exception, Inner Exception: {"Invalid object name 'dbo.Projects'."} I am using entityframeworks codefirst structure and here is my code:

[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'. is an exception from sql server. It means your table does not exist in the dbo schema.

Check that you are connected to the correct database and that migrations have been run.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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