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>
}
`
答案 0 :(得分:0)
正如@ Ivan-stoev所提到的,当你不需要时,你正在使用.Include()
。
.Include()
用于在同一数据库事务中包含链接的导航属性(相关实体) - 您只需返回一个列表。
尝试
var saloane = _context.Saloane.ToList();
或者,如果要过滤列表,请使用:
var saloane = _context.Saloane.Where(c => c.SomeProperty == "something").ToList();