【发布时间】:2017-07-16 16:35:44
【问题描述】:
EntityFramework.SqlServer.dll 中出现“System.InvalidOperationException”类型的异常,但未在用户代码中处理 我不知道该怎么做。我有一张照片,告诉我哪里没问题。
`---------------------Controller
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AppTest.Models;
using System.Web.Mvc;
using AppTest.ViewModels;
using System.Data.Common;
using System.Data.Entity;
namespace AppTest.Controllers
{
public class HomeController : Controller
{
private ApplicationDbContext _context;
public HomeController()
{
_context = new ApplicationDbContext();
}
protected override void Dispose(bool disposing)
{
_context.Dispose();
}
public ViewResult Index()
{
var saloane = _context.Saloane.Include(c => c.Id).ToList();
return View(saloane);
}
public ActionResult Details (int id)
{
var salon = _context.Saloane.SingleOrDefault(c => c.Id == id);
if (salon == null)
return HttpNotFound();
return View(salon);
}
--------------------------------Index
@model IEnumerable<AppTest.Models.Salon>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Saloane</h2>
@if (1>1)
{
<p>We don't have any customers yet.</p>
}
else
{
<table class="table table-bordered table-hover">
<thead>
<tr>
<th>Saloane</th>
</tr>
</thead>
<tbody>
@foreach (var salon in Model)
{
<tr>
<td>@Html.ActionLink(salon.Nume,"Adresa", "Id_Oras",new { id = salon.Id }, null)</td>
</tr>
}
</tbody>
</table>
}
`
【问题讨论】:
-
Include用于导航属性Id只是一个常规属性。 -
异常消息是不言自明的。
Include(c => c.Id)无效,删除即可。 -
我也有同样的问题。
标签: c# asp.net .net model-view-controller