[英]Mvc Invalid operation exception
我需要根據傳遞的id參數從my-sql服務器數據庫中檢索員工。 例如: 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);
}
}
}
單個方法返回序列中的唯一元素,如果序列中不存在一個元素,則引發異常。 因此您可以使用SingleOrDefault函數,如果您有任何所需的行,則該函數將返回null。
Employee employee = EmployeeContext.Employees.SingleOrDefault(emp => emp.EmployeeID == id);
InvalidOperationException輸入序列包含多個元素。 -或者-輸入序列為空。
我相信您要么有一個以上的雇員,要么沒有一個符合條件的雇員,所以拋出了異常。
如果可能存在多個Employee
,則應該使用First()
,或者使用SingleOrDefault
並檢查null
。
而不是使用Single
方法,而是使用“ Find
,然后驗證是否已找到它。
Employee employee = EmployeeContext.Employees.Find(id);
if (employee == null)
// not found on database
根據文檔 ,僅當有一條符合您的條件的記錄時, Single
方法才會成功。 不少於一項記錄。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.