I've just started my first MVC app following a tutorial. It seems to me that my code matches the tutors exactly however I'm getting an error:
'System.Data.SqlClient.SqlException: Invalid object name 'dbo.Employees'.' '[SqlException (0x80131904): Invalid object name 'dbo.Employees'.]'
My database is called 'Sample', and the web config reflects this.
Can anyone see my obvious mistake?
thanks
Employee Model
namespace MvcApplication2.Models
{
public class Employee
{
public int EmployeeId { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
}
}
Employee Controller
namespace MvcApplication2.Controllers
{
public class EmployeeController : Controller
{
public ActionResult Details(int id)
{
EmployeeContext employeeContext = new EmployeeContext();
Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id);
return View(employee);
}
}
}
EmployeeContext.cs Model
namespace MvcApplication2.Models
{
[Table("tblEmployee")]
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
}
}
WebConfig
<connectionStrings>
<add name="EmployeeContext" connectionString="Data Source={servername};Initial Catalog=Sample;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Global.asax
Database.SetInitializer<MvcApplication2.Models.EmployeeContext>(null);