我剛剛在教程後開始了我的第一個MVC應用程序。在我看來,我的代碼正好與教師匹配,但是出現錯誤:MVC實體框架 - 連接到數據庫
'System.Data.SqlClient.SqlException:無效的對象名稱'dbo.Employees'。' '[SqlException(0x80131904):無效的對象名稱'dbo.Employees'。]'
我的數據庫被稱爲'Sample',web配置反映了這一點。
任何人都可以看到我明顯的錯誤嗎?
感謝
僱員模型
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; }
}
}
僱員控制器
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模型
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);
嗨。不,我已經創建了SQL服務器數據庫。我的Global.asax文件包含:'Database.SetInitializer(null);' –
MJay