因此,我在这里按照教程进行逐步操作,无法摆脱此异常。我的连接字符串看起来不错。本教程中没有发生这种情况,所以我不知道出了什么问题。 它使我进入这一行Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ATSTest.Models;
namespace ATSTest.Controllers
{
public class EmployeeController : Controller
{
// GET: Employee
public ActionResult Details(int id)
{
EmployeeContext employeeContext = new EmployeeContext();
Employee employee = employeeContext.Employees.Single(emp => emp.EmployeeId == id);
return View(employee);
}
}
}
这是我的班级
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace ATSTest.Models
{
[Table("Employees")]
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string HiredDate { get; set; }
}
}
连接字符串
<connectionStrings>
<add name ="EmployeeContext" connectionString="Data Source=(LocalDb)\MSSQLLocalDB;Initial Catalog=AssetTracking;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
EmployeeContext类
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace ATSTest.Models
{
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
}
}
答案 0 :(得分:0)
您的问题的答案是您没有使用连接字符串。
您将必须传递连接字符串名称,如下例所示。
public class EmployeeContext : DbContext
{
public EmployeeContext()
: base("EmployeeContext")
{
}
// DbSet's here
publlic DbSet<Employee> Employees { get; set; }
}