我需要从my-sql server数据库中检索员工,具体取决于传递的id参数。例如:http://localhost:66666/employee/details/1 <---the parameter
并将参数与数据库中的id匹配,并在视图中显示。出于某种原因,我在员工控制器中的此行上收到了无效的操作异常: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 MVCTutorials.Models;
namespace MVCTutorials.Controllers
{
public class EmployeeController : Controller
{
// GET: Employee
public ActionResult Details(int id)//id =paramenter
{
EmployeeContext EmployeeContext = new EmployeeContext();
Employee employee = EmployeeContext.Employees.Single(emp => emp.EmployeeID == id);
return View(employee);
}
}
}
员工模式
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace MVCTutorials.Models
{
[Table("TblEmp")]
public class Employee
{
[Key]
public int EmployeeID { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public string City { get; set; }
}
}
员工观点
@model MVCTutorials.Models.Employee
@{
ViewBag.Title = "Employeee Details";
}
<h2>Employeee Details</h2>
<table style="font-family:Arial;">
<tr><!--The Row-->
<td>
<b>Employee Id: </b>
</td>
<td>
@Model.EmployeeID
</td>
</tr>
<tr>
<td>
Name:
</td>
<td>
@Model.Name
</td>
</tr>
<tr>
<td>Gender:</td>
<td>@Model.Gender</td>
</tr>
<tr>
<td>City:</td>
<td>@Model.City </td>
</tr>
</table>
员工背景
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace MVCTutorials.Models
{
public class EmployeeContext : DbContext
{
public DbSet<Employee> Employees { get; set; }
}
}
Global.aspx
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace MVCTutorials
{
public class MvcApplication : System.Web.HttpApplication
{
//Happens when appplication first starts
protected void Application_Start()
{
//Purpose used to initalize your database e.g if you dont have a database created already this will create one for you(database,tables and data will all be created)
// we pass null saying we dont want any of this
Database.SetInitializer<MVCTutorials.Models.EmployeeContext>(null);
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
}
}
答案 0 :(得分:1)
Single方法返回序列的唯一元素,如果序列中没有一个元素,则抛出异常。 所以你可以使用SingleOrDefault函数,如果你有任何想要的行,它返回null。
Employee employee = EmployeeContext.Employees.SingleOrDefault(emp => emp.EmployeeID == id);
答案 1 :(得分:0)
根据MSDN for Enumerable.Single():
出现InvalidOperationException 输入序列包含多个元素。 -要么- 输入序列为空。
我相信您有多个符合条件的员工或没有符合条件的员工,因此会抛出异常。
如果可能存在多个First()
,则应使用Employee
或使用SingleOrDefault
并检查null
。
答案 2 :(得分:0)
不使用Single
方法,而是使用Find
,然后验证是否找到了它。
Employee employee = EmployeeContext.Employees.Find(id);
if (employee == null)
// not found on database
作为per documentation,Single
方法只有在符合条件的情况下才会成功。不少于一条记录。