简体   繁体   中英

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.SqlServer.dll but was not handled in user code

An exception of type 'System.InvalidOperationException' occurred in EntityFramework.SqlServer.dll but was not handled in user code I don't know how to do. I have a pic where vs tel me where it't the problem.

图片链接

`---------------------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>
    }
    `

As mentioned by @Ivan-stoev, you're using .Include() when you dont need to.

.Include() is used to include linked navigation properties (related entities) in the same database transaction - you just need to return a list.

Try

var saloane = _context.Saloane.ToList();

or, if you want to filter your list, use:

var saloane = _context.Saloane.Where(c => c.SomeProperty == "something").ToList();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM